我试图从之前的ReportActivity中捕获的SD卡中获取图像。
这是我在ReportActivity中将图像保存到SD卡目录的代码。
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);
}
});
}
然后使用OnActivityResult拍摄照片。
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);
}
}
}
按钮转到下一页。
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);
}
然后,我显示图像的图像和URL。
下一页的onCreate方法。
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));
* /
}
然而结果是我无法加载图像。但是会显示图像的路径(String)。
(我无法在此处显示图片,因为我的声誉低于10)
请帮忙!
答案 0 :(得分:0)
借助Android 4.4(KitKat),谷歌阻止了应用程序写入SD卡,除非是非常具体的沙盒位置。
答案 1 :(得分:0)
尝试这种方式:
File imgFile = new File(imageURL);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView = (ImageView) findViewById(R.id.imageTesting);
imageView.setImageBitmap(myBitmap);
}