【问题标题】:How to open an image in drawable folder using android imageview applications?如何使用 android imageview 应用程序在可绘制文件夹中打开图像?
【发布时间】:2013-10-20 04:55:23
【问题描述】:

我想通过手机的默认 imageview 应用程序(图库等)打开可绘制文件夹中的图像。

我的图片位置:drawable/image.png

代码:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(""), "image/*");
startActivity(intent);

我需要在 Uri.parse 函数中写什么才能看到图像文件?

【问题讨论】:

    标签: android android-intent imageview


    【解决方案1】:

    为此,您必须将可绘制文件夹中的图像复制到 SD 卡,然后指定 SD 卡中文件的路径。

    代码如下:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.imagename);
    
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    
            File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg");
            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            Uri path = Uri.fromFile(f);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "image/*");
            startActivity(intent);
    

    请注意,imagename 是可绘制文件夹中图像的名称

    别忘了在manifest文件中添加权限以访问SD卡

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    【讨论】:

    【解决方案2】:

    我认为你做不到。此可绘制对象对您的应用程序是私有的,其他应用程序无法使用它。您应该创建自己的ImageViewer 或将这些drawable 复制到SD 卡并在启动Intent 时设置Uri

    【讨论】:

    • 我可以将文件夹从drawable切换到assets,这个文件夹可以吗?
    【解决方案3】:

    您可以通过此代码访问您的图片:

    R.drawable.image
    

    例如,您可以在“onCreate”方法中使用此代码:

    ImageView img = new ImageView(this);
    img.setImageDrawable(R.drawable.image);
    

    【讨论】:

    • 我不想用我的图像查看器打开图像文件。我想用 Gallery/QuickPic/Instagram 等打开它。
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多