将图像从文件夹加载到ImageView

时间:2018-05-07 06:13:46

标签: android

我正在开发一个捕获照片并将其保存在名为“photo”的内部设备文件夹中的应用程序。我需要将照片从“photo”文件夹加载到imageview。文件夹中只有一张照片。怎么办这个? 请帮帮我。谢谢你。 下面是我尝试过的代码,只有在显示照片名称时才从文件夹中加载照片。

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/GeoPark/final_photo/20180504_002754.jpg";
        File imgFile = new File(path);
        if (imgFile.exists()) {
            imageView.setImageBitmap(decodeFile(imgFile));
        }
        else
            Toast.makeText(ViewActivity.this,"No Image File Present ",Toast.LENGTH_SHORT).show();

2 个答案:

答案 0 :(得分:0)

你可以简单地搜索一下,保证结果。

无论如何,Glide将为您提供帮助

编辑:

使用相机拍照:

private void dispatchTakePictureIntent(Context context) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile(context);
        } catch (IOException ex) {
            // Error occurred while creating the File
            Snackbar.make(btn_open_camera, "Couldn't create a file for your picture!", Snackbar.LENGTH_LONG);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(context,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, MY_PERMISSIONS_REQUEST_CAMERA);
        }
    }
}

private File createImageFile(Context context) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = context.getFilesDir();
    Log.i(TAG, "StorageDir : " + storageDir);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    Log.i(TAG, "mCurrentPhotoPath : " + mCurrentPhotoPath);

    return image;
}

现在您将图像路径存储在mCurrentPhotoPath中,这是当前活动中声明的String。如你所说,你将在下一个活动中需要这个,为此你可以看看this answer

答案 1 :(得分:0)

尝试此方法..在下面的方法中给出正确的文件路径。

File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);

}

第二件事......

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

我希望你在android清单文件中添加以下权限..

New Referencing Outlet Collections