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

时间:2012-09-29 11:55:23

标签: android camera image-gallery

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

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);

这里最初用来显示2个画廊

1。摄像机

2。我的图像

选择他们中的任何一个......,一切正常。

但我无缘无故地删除了my-images文件夹并再次运行了该应用程序。将再次创建相同的文件夹,并按预期保存图像。

问题是:

当我点击选择图片按钮时,

我的图片库现在没有显示。

除了删除my-images文件夹并重新启动应用程序外,我还没有更改任何代码,因为它曾经运作良好。

为什么会这样?

谢谢

修改

我的活动代码,允许用户拍照并存储在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 个答案:

答案 0 :(得分:4)

可能是因为图库与您的应用程序相关联并且使用它来保持视图。 因此,当您删除从系统中删除的图像/文件夹时会发生什么,但未更新与您的应用程序关联的媒体库。 请使用以下代码,如果这可以解决您的问题,请告诉我

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;
    }