我是android的新手。我的问题是如何在图像视图中设置共享首选项。我想将图像分享给另一个活动。请帮帮我,因为我已经备了..请帮我解释清楚和代码。谢谢。
答案 0 :(得分:0)
跨活动共享数据的“标准”方法是在intent类上使用putExtraXXX方法。您可以将图像路径放在意图中:
Intent intent = new Intent(this,MyClassA.class);
intent.putExtra(MyClassA.IMAGE_EXTRA, imagePath);
startActivity(intent);
然后检索它并在下一个活动中打开它:
String filePath = getIntent().getStringExtra(MyClassA.IMAGE_EXTRA);
这是一个函数的实现,它打开并解码图像并返回一个Bitmap对象,注意这个函数要求图像位于assets文件夹中:
private Bitmap getImageFromAssets(String assetsPath,int reqWidth, int reqHeight) {
AssetManager assetManager = getAssets();
InputStream istr;
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
istr = assetManager.open(assetsPath);
bitmap = BitmapFactory.decodeStream(istr, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(istr, null, options);
} catch (IOException e) {
return null;
}
return bitmap;
}