代码之家  ›  专栏  ›  技术社区  ›  Archie.bpgc

为什么新创建的文件夹中的图像没有显示?

  •  1
  • Archie.bpgc  · 技术社区  · 12 年前

    在我的应用程序中,我可以选择拍照,并将其保存在文件夹中:

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/my-images";
    

    图像保存得很好,我还可以选择使用此代码的图像:

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    

    这里最初有两个画廊

    1.摄像头

    2.我的图片

    随便挑一个等等,一切都很顺利。

    但毫无理由,我删除了我的图像文件夹,并再次运行应用程序。将再次创建相同的文件夹,并按预期保存图像。

    问题在于:

    当我单击“选择图像”按钮时,我的图像库现在没有显示。

    除了删除我的图像文件夹并重新启动应用程序外,我没有更改任何代码,因为它过去工作得很好。

    为什么会这样?

    非常感谢。

    编辑:

    我的活动代码,允许用户拍照并存储在SD卡上,以及浏览图片库:

    public class PictureFromAppActivity extends SherlockActivity {
        private File dir;
        private Bitmap photo;
        private String encodedString;
        private InputStream is;
        private ImageView imagePreview;
        private String selectedImagePath;
        private static final int CAMERA_REQUEST = 1888;
        private static final int SELECT_PICTURE = 1;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            imagePreview = (ImageView) findViewById(R.id.image_preview);
            Button takePicture = (Button) findViewById(R.id.take_a_picture);
            Button selectImage = (Button) findViewById(R.id.select_picture);
    
            takePicture.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View arg0) {
    
                    if (android.os.Environment.getExternalStorageState().equals(
                            android.os.Environment.MEDIA_MOUNTED)) {
    
                            Intent cameraIntent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(cameraIntent, CAMERA_REQUEST);
                        }
                }
            });
            selectImage.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
    
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(
                            Intent.createChooser(intent, "Select Picture"),
                            SELECT_PICTURE);
                }
            });
    
        }
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == RESULT_OK) {
                if (requestCode == CAMERA_REQUEST) {
                    if (data != null) {
    
                        photo = (Bitmap) data.getExtras().get("data");
                        Bitmap bitmapOrg = photo;
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao);
                        byte[] ba = bao.toByteArray();
    
                        try {
                            encodedString = Base64.encodeBytes(ba, 0);
                        } catch (IOException e1) {
                        }
    
                        if (android.os.Environment.getExternalStorageState()
                                .equals(android.os.Environment.MEDIA_MOUNTED)) {
    
                            File imagesFolder = new File(
                                    Environment
                                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                                    "/my-images");
                            if (!imagesFolder.exists()) {
    
                                imagesFolder.mkdirs();
                            } else {
    
                            }
                            String fileName = "image_" + count + ".jpg";
                            f = new File(imagesFolder, fileName);
                            while (f.exists()) {
                                count++;
                                fileName = "image_" + String.valueOf(count)
                                        + ".jpg";
                                f = new File(
                                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                                + "/my-images", fileName);
                            }
                        } else {
    
                        }
                        f.createNewFile();
    
                        FileOutputStream fo = new FileOutputStream(f);
                        fo.write(ba);
                        o.flush();
                        fo.close();
                        try {
                            File imageFile = new File(
                                    Environment
                                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                            + "/my-images" + "/image_" + count + ".jpg");
                            Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                                    .getAbsolutePath());
                            imagePreview.setImageBitmap(bitmap);
                            Log.d("Image Width", "+" + imagePreview.getWidth());
                            Log.d("Image Height", "+" + imagePreview.getHeight());
                        } catch (Exception e) {
    
                        }
                    } else {
    
                        Intent intent = new Intent(PictureFromAppActivity.this,
                                PictureFromAppActivity.class);
                        startActivity(intent);
                    }
                } else if (requestCode == SELECT_PICTURE) {
    
                    if (data != null) {
                        Uri selectedImageUri = data.getData();
                        selectedImagePath = getPath(selectedImageUri);
                        try {
                            File imageFile = new File(selectedImagePath);
                            Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                                    .getAbsolutePath());
                            imagePreview.setImageBitmap(bitmap);
                        } catch (Exception e) {
    
                        }
    
                    } else {
    
                    }
                }
            }
        }
    
        private String getPath(Uri selectedImageUri) {
    
             String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
        }
    }
    
    1 回复  |  直到 12 年前
        1
  •  5
  •   Abhinav Singh Maurya khushal rasali    12 年前

    这可能是因为图库与您的应用程序相关联,并与它保持视图。 因此,当您删除图像/文件夹时会发生什么?它已从系统中删除,但与您的应用程序相关的媒体库没有更新。 请使用以下代码,并让我知道这是否解决了您的问题

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
    

    请使用此代码打开库中的图像

    intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(intent, PICK_PHOTO);
    

    在你的活动结果中

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            String path = "";
    switch (requestCode) {
            case PICK_PHOTO:
                if (resultCode == RESULT_OK) {
                   Uri uri = data.getData();
                   path = pathFromUri(uri); // Now you can get the path of your selected image from here
               }
            break;
            }
        }
    

    从URI代码获取路径

    /**
         * Method to get path from uri
         * 
         * @param Uri
         *            uri
         * @return String path
         * */
        private String pathFromUri(Uri uri) {
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null,
                    null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            return filePath;
        }