I was trying to get image from SD Card which I captured in previous ReportActivity.
This is my code to save image into SD Card directory in ReportActivity.
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
currentPicLoc=root+"/saved_images"+fname;
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
Then OnActivityResult for capturing a photo.
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data.hasExtra("data")){
Bitmap thumbnail =(Bitmap)data.getExtras().get("data");
buttonLoadImage.setImageBitmap(thumbnail);
SaveIamge(thumbnail);
}else{
Toast.makeText(getApplicationContext(), "No image", Toast.LENGTH_LONG);
}
}
}
Button to go next page.
public void onSubmit(View view) {
Toast.makeText(getApplicationContext(), currentPicLoc, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,Testing.class);
intent.putExtra("imageURL",currentPicLoc);
startActivity(intent);
}
Then, I display the image and URL for the image.
onCreate method in next page.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
Bundle extras = getIntent().getExtras();
String imageURL = extras.getString("imageURL");
TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
tvMessage.setText(imageURL);
ImageView imageView = (ImageView) findViewById(R.id.imageTesting);
Bitmap thumbnail = (BitmapFactory.decodeFile(imageURL));
imageView.setImageBitmap(thumbnail);
/*
File file = new File (imageURL);
imageView.setImageURI(Uri.fromFile(file));
*/
}
However the result is i cannot load the image. But the path(String) of the image is displayed.
(I can't display image here as my reputation is below 10)
Please Help!
使用Android 4.4(KitKat),Google阻止了应用程序写入SD卡的操作,除了特定的沙盒位置。
Try this way:
File imgFile = new File(imageURL);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView = (ImageView) findViewById(R.id.imageTesting);
imageView.setImageBitmap(myBitmap);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.