为什么我的应用程序无法找到新拍摄的图像?

时间:2021-02-05 20:40:53

My app allow user to take picture and immediately so it in the gallery (all pictures in external storage). However, I couldn't view my newly taken picture in the gallery. I am sure the picture is taken, as I can view it in file manager. And the path is correct. Why can't my app recognize the picture it just took. What do I miss?

我的应用程序允许用户拍摄照片,并立即将其拍摄到库中(外部存储中的所有照片)。但是,我无法在画廊中查看我新拍摄的照片。我确信这张照片已被拍摄,因为我可以在文件管理器中查看它。路径是正确的。为什么我的应用无法识别它刚刚拍摄的照片。我错过了什么?

Permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

The code

@Override
public void onClick(View v) { // the listener
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(photoFile != null) {
        Uri uri = Uri.fromFile(photoFile);
        ImageSelectionActivity.photoUri = uri;
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        ((Activity)context).startActivityForResult(intent, TAKE_PIC_REQUEST);
    }
}


private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "IMG_" + timeStamp + "_";
    String dir = Environment.getExternalStorageDirectory() + "/DCIM/MyApp";
    File storageDir = new File(dir);
    if(!storageDir.exists()) {
        storageDir.mkdir();
    }
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);
    return image;
}

// The CursorLoader
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // Get relevant columns for use later.
    String[] projection = {
            MediaStore.Files.FileColumns._ID,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.TITLE,
            MediaStore.Images.Media.DATA
    };

    Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    CursorLoader cursorLoader = new CursorLoader(
            this,
            queryUri,
            projection,
            null,
            null, // Selection args (none).
            MediaStore.Images.Media.DATE_ADDED + " DESC" // Sort order.
    );
    return cursorLoader;
}

1 个解决方案

#1


0  

I would enter ImageView/TextView(Placeholder) in the same Activity use this function.

我会在同一个Activity中使用此函数输入ImageView / TextView(占位符)。

@Override
protected void onActivityResult (int requestCode, int resultCode, 
Intent data) {  
if (requestCode == RESULT_OK)   {
Bitmap photo = (Bitmap)data.getExtras().get("data");
((ImageView) findViewById(R.id.imagsView)).setImageBitmap(photo);

 }
 }

this brings much more then viewing, log. becouse there is much more data you can pass. you can see/regonizeo, if every code dos correctly to the ImageView/TextView. From the ImageView/Textview on, you can Test Methods, to save Data with the final Information you would to use from the TextView. Some ideas from *, would me help also. maybee someone write, good enought to understand, answers on your question.

这带来了更多,然后查看,日志。因为你可以通过更多的数据。你可以看到/ regonizeo,如果每个代码都正确地使用ImageView / TextView。从ImageView / Textview上,您可以使用测试方法,使用从TextView中使用的最终信息来保存数据。 *的一些想法,我也会帮忙。也许有人写,善于理解,回答你的问题。

#1


0  

I would enter ImageView/TextView(Placeholder) in the same Activity use this function.

我会在同一个Activity中使用此函数输入ImageView / TextView(占位符)。

@Override
protected void onActivityResult (int requestCode, int resultCode, 
Intent data) {  
if (requestCode == RESULT_OK)   {
Bitmap photo = (Bitmap)data.getExtras().get("data");
((ImageView) findViewById(R.id.imagsView)).setImageBitmap(photo);

 }
 }

this brings much more then viewing, log. becouse there is much more data you can pass. you can see/regonizeo, if every code dos correctly to the ImageView/TextView. From the ImageView/Textview on, you can Test Methods, to save Data with the final Information you would to use from the TextView. Some ideas from *, would me help also. maybee someone write, good enought to understand, answers on your question.

这带来了更多,然后查看,日志。因为你可以通过更多的数据。你可以看到/ regonizeo,如果每个代码都正确地使用ImageView / TextView。从ImageView / Textview上,您可以使用测试方法,使用从TextView中使用的最终信息来保存数据。 *的一些想法,我也会帮忙。也许有人写,善于理解,回答你的问题。