-1

在我的应用程序中,我正在从列表视图中的 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();
    }
}
4

2 回答 2

2

发送活动

final String root = Environment.getExternalStorageDirectory().getAbsolutePath();
pathToImage = root + "/my/image/path/image.png";

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("imagePath", pathToImage);
startActivity(intent);

在您接受活动时:

String path = getIntent().getStringExtra("imagePath");
Drawable image = Drawable.createFromPath(path);
myImageView.setImageDrawable(image);
于 2012-10-15T09:38:55.797 回答
2

使用它来将图像存储在 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()); 

通过意图发送图像并使用

String image_path = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
myimageview.setImageDrawable(bitmap);

在您的接收活动中将图像显示到名为 myimageview 的图像视图上

根据评论,看起来应该是 myimageview.setImageBitmap(bitmap) 。没有测试这个。但是如果上述方法不起作用,也可以尝试一下

于 2012-10-15T10:01:46.813 回答