如何从Android studio中的网格视图中选择图像路径

时间:2017-04-12 11:02:58

标签: android gridview

我想要从gridview中选择图像的路径并将其发送到另一个活动以对所选图像应用过滤器。

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                currentImageViewId = v.getId();
                System.out.println("ID !!!!!!!!!!"+position);

                    Bitmap selectedPic =  SlideApplication.bitmapList.get(position);
                    Intent intent = new Intent(gridEditor.this, Photo_Effects.class);
                    intent.putExtra("imageUri", selectedPic.toString());
                    startActivityForResult(intent,Request_Code);
                    Toast.makeText(gridEditor.this, "" + position, Toast.LENGTH_SHORT).show();

            }

        });

此代码只是让我查看ID。我想要所选图像的路径。

2 个答案:

答案 0 :(得分:0)

您需要在正确之前将Bitmap转换为Uri或String。

试试这个方法:

public File bitmapToFile(Bitmap bit, String filename) {

    File f = new File(YourClassContext.getCacheDir(), filename);
    try {
        f.createNewFile();


        //Convert bitmap to byte array
        Bitmap bitmap = bit;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f;

}

然后你通过Uri file = Uri.fromFile(bitmapToFile(bitmap, name));

获得了Uri

答案 1 :(得分:0)

如何加载此gridview?如果您将其保存在散列图或类似内容中,则可以检查this解决方案