如何将图像从一个活动发送到另一个活动(使用文件路径)
在我的应用中,我正在从列表视图中的url加载图像.我想在下一个活动中显示列表项的onclick.我可以通过意图传递位图,但是考虑到可以通过意图发送的数据的大小限制,我不想这样发送.
In my app, I am loading images from url's in listview. And onclick of list item I want to show it on next activity. I can pass bitmap through intent,But considering the size restriction of data that can be send through intent I dont want to send this way.
任何人都知道将图像从一种活动传递到另一种活动的更好方法.
Anybody knows the better way of passing image from one activity to other.
我听说过将图像存储在文件中并使用intent发送文件路径的消息,但是不知道如何?请告诉我该怎么做?
I have heard about storing image in file and sending filepath using intent But don't know how?Please tell me how can I do it?
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
imageview=(ImageView) view.findViewById(R.id.icon);
description=(TextView) view.findViewById(R.id.firstLine);
rating=(TextView) view.findViewById(R.id.text1);
noofDownloads=(TextView) view.findViewById(R.id.text2);
noofComments=(TextView) view.findViewById(R.id.text3);
imageId=(TextView) view.findViewById(R.id.imageIdText);
publishdate=(TextView) view.findViewById(R.id.thirdLine);
attribution=(TextView) view.findViewById(R.id.attributionText);
Intent intent = new Intent(PicturesList.this, PictureDetail.class);
String fileName=description.getText().toString();
fileclass=new FileClass();
fileclass.saveImage(bitmap,fileName);
Intent intent = new Intent(PicturesList.this, PictureDetail.class);
intent.putExtra("imagePath",fileclass.getPath());
intent.putExtra("Description",description.getText());
intent.putExtra("Rate",rating.getText());
intent.putExtra("Downloads",noofDownloads.getText());
intent.putExtra("Comments",noofComments.getText());
intent.putExtra("PublishTime",publishdate.getText());
startActivity(intent);
}
});
}
我已经从ListAdapter保存了图像
And I have save images from ListAdapter
在
getview()
{
String fileName=lolpic.getDescription().toString();
FileClass fileclass=new FileClass();
fileclass.saveImage(bitmap,fileName);
和FileClass.java
and FileClass.java
public class FileClass {
Picture pic;
File file;
public void saveImage(Bitmap myBitmap,String fileName) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pictures");
String fname = fileName+".png";
file = new File (myDir, fname);
if (file.exists ())
{
file.delete ();
}
try {
FileOutputStream out = new FileOutputStream(file);
//myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
out.write(byteArray);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getPath()
{
return file.getPath();
}
}
使用此将图像存储在sdcard中
use this to store image in sdcard
void saveImage() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
String fname = "MyImage.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
然后从 file.getpath()
并使用
intent.putExtra("imagePath", file.getpath());
通过意图发送图像并使用
to send the image through intent and use
String image_path = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
myimageview.setImageDrawable(bitmap);
在您的接收活动中将图像显示在名为myimageview的imageview上
in your receiving activity to display an image onto the imageview named myimageview
基于注释,看起来应该是myimageview.setImageBitmap(bitmap).没有测试.但是,如果上面的方法不起作用,也可以尝试一下
Based on comments, looks like it should be myimageview.setImageBitmap(bitmap) . Didn't test this. But give a try to this also if above doesn't work