GridView Image (from URL) to Second Activity by using Intent

时光怂恿深爱的人放手 提交于 2020-01-06 10:42:24

问题


First of all im giving the codes what i'm using currently

MainActivity

    static final String URL = "http://my .com/images/rss.xml";
    static final String KEY_TITLE = "item";
static final String KEY_THUMB_URL = "thumb_url";
// Click event for single list row
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @SuppressWarnings("unchecked")
            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {


                HashMap<String, String> map2 = (HashMap<String, String>) parent.getItemAtPosition(position);

                Intent in = new Intent(getApplicationContext(), FullSize.class);
                Bitmap b; // your bitmap

                in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
                in.putExtra(KEY_THUMB_URL, KEY_THUMB_URL);

                startActivity(in);
             }
         });

2nd Activity

@Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullsize);

        ImageView image = (ImageView) findViewById(R.id.fullsizeimg2);
        TextView txtName = (TextView) findViewById(R.id.txt1);
        Intent in = getIntent();
        // Receiving the Data

        String name = in.getStringExtra("item"); 
        Bitmap bitmap =(Bitmap) in.getParcelableExtra("thumb_url");

        // Displaying Received data
        txtName.setText(name);
        image.setImageBitmap(bitmap);




    }
}

in this case, if i use the codes as above , the title works , i can see the text in txt but i cannot get img. i think i need to convert it to bitmap but also it didnt work for me. for converting bitmap i used this

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),"Image ID");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
////////for intent /////
intent.putExtra("imagepass", bytes.toByteArray());
/////////2nd activity//////////
Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("imagepass");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    ImageView iv=(ImageView) findViewById(R.id.fullsizeimg);
    iv.setImageBitmap(bmp);

but in main activity after decoderesource line, it was giving this error : The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, String)

I will be very happy if you can help.


回答1:


You are using String as the second parameter in BitmapFactory.decodeResource() But according to your code the Bitmap creation should be like this Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageId);



来源:https://stackoverflow.com/questions/22132263/gridview-image-from-url-to-second-activity-by-using-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!