1

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

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

1 回答 1

5

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

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;
    }
于 2012-09-29T13:59:44.207 回答