将图像从内部存储复制到Android Java库中?

时间:2019-03-13 20:12:40

标签: android

我正在将图像存储在我的Android应用程序的内部存储中。我也想在图库中显示这些图像。我尝试过,但是在图库中看不到图像。我需要尽快解决。

强文本

String currentPhotoPath;
File  imageFile=null;
private void openCamera() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    if(intent.resolveActivity( getPackageManager())!=null){


        try {
            imageFile=createImageFile();

        } catch (IOException e) {
            e.printStackTrace();
        }

        if (imageFile!=null){
            Uri imageUri= FileProvider.getUriForFile( this,"com.example.android.fileprovider",imageFile );
            intent.putExtra( MediaStore.EXTRA_OUTPUT,imageUri );
            startActivityForResult(intent,100);
          //here am calling gallery to broadcast
            galleryAddPic();
        }
    }

}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir( Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

public void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File( String.valueOf( imageFile ) );
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

0 个答案:

没有答案