【问题标题】:Load images from folder to ImageView将图像从文件夹加载到 ImageView
【发布时间】:2018-05-07 06:13:45
【问题描述】:

我正在开发一个捕获照片并将其保存在名为“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();

【问题讨论】:

标签: android


【解决方案1】:

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

private void loadImageFromStorage(String path)
{

try {
    File f=new File(path, "profile.jpg");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(R.id.imgPicker);
    img.setImageBitmap(b);
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}

}

第二件事..

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

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

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

【讨论】:

  • 我想从文件夹中加载所有照片而不指定名称
  • 如果您使用单个图像视图,它一次只显示一个图像。
  • 该文件夹将具有不同的图像名称,因为图像名称是一个时间戳。那么如何指定名称
  • 图片会不时变化,如何提前知道图片名称
  • 图片名是必需的,没有图片名如何知道imageview要加载哪个图片。
【解决方案2】:

您可以简单地对此进行搜索,保证结果。

不管怎样,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

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多