在我的应用程序中,我正在从列表视图中的 url 加载图像。并且单击列表项我想在下一个活动中显示它。我可以通过意图传递位图,但是考虑到可以通过意图发送的数据的大小限制,我不想以这种方式发送。
任何人都知道将图像从一个活动传递到另一个活动的更好方法。
我听说过将图像存储在文件中并使用意图发送文件路径但不知道如何?请告诉我该怎么做?
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 保存图像
在
getview()
{
String fileName=lolpic.getDescription().toString();
FileClass fileclass=new FileClass();
fileclass.saveImage(bitmap,fileName);
和 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();
}
}