0

在我的应用程序中,当启动画面启动时,我正在从 URL 下载图像。我想在我的应用程序的另一个活动中使用相同的图像。以下是我下载图像的代码

public void DownloadImage(String fileName)      
    {

        try 
        {
                           URL url = new URL(main.BannerImage); //you can write here any link                          
                           File file = new File(fileName);
                           Log.e("file ",""+file);

                           URLConnection ucon = url.openConnection();                          
                           InputStream is = ucon.getInputStream();                         
                           BufferedInputStream bis = new BufferedInputStream(is);
                           ByteArrayBuffer baf = new ByteArrayBuffer(50);
                           int current = 0;
                           while ((current = bis.read()) != -1) 
                           {
                                 baf.append((byte) current);
                           }

                           FileOutputStream fos = new FileOutputStream(file);
                           fos.write(baf.toByteArray());
                           fos.close();
        }
        catch (IOException e) 
        {
                            Log.e("Error: ","" + e);
        }

如何在另一个活动中将图像作为背景源请帮助我的朋友

4

1 回答 1

0

您可以将图像保存在 SDCard 中,并且可以使用将路径发送到下一个活动

intent.putExtras("filename","filepathname"); 

并使用

getIntent().getExtras().getString("filename") 

从这里你可以从以前的活动中获取文件路径,你可以从特定的文件路径中获取图像。

您可以将图像转换为位图,然后使用 Parcelable 将其传递给下一个活动

Bundle extras = new Bundle(); 
extras.putParcelable("data",bitmap);
Intent intent=new Intent(currentActivity.this,nextImage.class); 
intent.putExtras(extras);
startActivity(intent);
finish();

在下一个活动中,您可以获得位图

 Bitmap  image=getIntent().getExtras().getParcelable("data");
于 2011-07-12T09:30:45.917 回答