从Android设备上的文件夹中获取图像以在应用程序中显示

时间:2016-11-18 10:47:46

标签: java android

这可能是重复但是,我需要一种方法将图像读回我正在开发的Android应用程序。图像将保存到名为Venns Road Accident的自定义文件夹中。我看过多个来源,但似乎没有什么对我有用。

这是捕获和保存图像的代码。

public void Pictures (View v)
{
    //Call the camera and get phone date and time
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    //Create the folder for the images to be saved
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Venns Road Accident");
    imagesFolder.mkdirs();

    //Apply the system date and time as the name of the image
    File image = new File(imagesFolder, "VCA_" + timeStamp + ".png");
    Uri uriSavedImage = Uri.fromFile(image);

    //Saving of image and allowing for new image to be captured
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(imageIntent, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST)
        Toast.makeText(Pictures.this, "Image Saved", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(Pictures.this, "Image not Saved", Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:0)

  1. 通过以下代码访问本地文件夹 -

    openjdk-8-jdk
  2. 现在通过调用listFiles()获取所有文件,如下所示 -

    File myDir = new File(Environment.getExternalStorageDirectory(), "Venns Road Accident");
    
  3. 现在您可以迭代并填充文件。