在我的应用程序中,我正在拍照,并在压缩后成功将其保存在库中。现在我想将其展示到其他活动中,以便用户可以共享或至少查看它。那我该怎么做呢
以下是我保存图片的代码,保存后,它显示广告,在adClosed事件中,我想将拍摄的照片发送到其他活动,我该怎么做。我的代码就是这样..
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAnimals");
storagePath.mkdirs();
String finalName = Long.toString(System.currentTimeMillis());
//this snippet is saving image And I am showing ad after saving picture
File myImage = new File(storagePath, finalName + ".jpg");
String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/" + finalName + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
//refreshing gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(myImage));
sendBroadcast(mediaScanIntent);
} catch (IOException e) {
Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();
displayInterstitial();
interstitial.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
Log.v("Add time");
Intent intent = new Intent(CameraActivity.this,ShowCapturedImage.class);
//Now How to send the saved picture to the image view of other activity?
startActivity(intent);
super.onAdClosed();
}
});
答案 0 :(得分:2)
1)将拍摄的图像路径置于意图
2)获取其他活动中的路径并在imageview中设置
public static final int REQUEST_CODE_FROM_CAMERA = 112;
private Uri fileUri;
String image_path = "";
//从功能下方抓取图片
private void fromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Log.d("FROM CAMERA CLICKED file uri", fileUri.getPath());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);
}
//在活动结果商店图片路径
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FROM_CAMERA
&& resultCode == Activity.RESULT_OK) {
try {
image_path = fileUri.getPath();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
点击任意按钮
Intent iSecond=new Intent(FirstActivity.this,SecondActivity.class);
iSecond.putExtra("image_path",image_path);
startActivity(iSecond);
在第二个活动onCreate()
Bundle extras = intent.getExtras();
if(extras != null)
String image_path = extras.getString("image_path");
从此图片路径,您可以获取图片并设置为imageview
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
File imgFile = new File("/storage/emulated/0/1426484497.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
iv.setImageBitmap(myBitmap);
}
}
答案 1 :(得分:0)
您可以通过向活动添加ImageView来实现此目的。
简单的ImageView应如下所示:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
然后在Activity类中捕获它
ImageView imageView = (ImageView) this.findViewById(R.id.imageView);
现在有趣的部分。我们将使用图像URI捕获您的图像,并将其解析为位图。
Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath()); //Here goes your image path
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap,imageView.getWidth(), imageView.getHeight(), false)); //I scale the bitmap so it show properly. If the image is too big, it wont show on the ImageView
应该这样做,告诉我它是否有效!