Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Image with intent from internal storage

Tags:

android

I want to open an image from internal folder with the android default image viewer, on the Nexus 7 tablet. I use the following code,but for some reason the image is not displayed. What I'm doing wrong? The path to the file is :

file:///data/data/com.example.denandroidapp/files/Attachments/photoTemp/photo.jpg

(this is what Uri.parse("file://" + file) returns).

ArticlePhoto photo =  new ArticlePhoto(soapObject);
File f = new File(context.getFilesDir() + "/Attachments/photoTemp");

if(!f.exists())
    f.mkdirs();

if (photo.ArtPhoto != null) {
    Bitmap articlePhoto = BitmapFactory.decodeByteArray(photo.ArtPhoto, 0, photo.ArtPhoto.length);                      
    ByteArrayOutputStream  bytesFile  =  new ByteArrayOutputStream();
    articlePhoto.compress(Bitmap.CompressFormat.JPEG, 100, bytesFile);

    File file = new File(f + "/photo.jpeg");

    try {
        if(!file.exists())
            file.createNewFile();

        FileOutputStream outStream =  new FileOutputStream(file);

        outStream.write(bytesFile.toByteArray());                  
        outStream.close();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://" + file),"image/jpeg"); 
        startActivity(intent);

    } catch(Exception ex) {
        AlertDialog alert =  new  AlertDialog.Builder(context).create();
        alert.setTitle("Warning!");
        alert.setMessage(ex.getMessage());
        alert.show();
    }
}
like image 839
Roman Marius Avatar asked Jan 23 '13 14:01

Roman Marius


3 Answers

Try with this :

    Intent intent = new Intent();  
    intent.setAction(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.parse("file://" + file.getAbsolutePath());                 
    intent.setDataAndType(uri,"image/*");
    startActivity(intent);

Thanks.

like image 135
Pratik Sharma Avatar answered Nov 05 '22 05:11

Pratik Sharma


The problem is that the image is internal to your application! So an external application (Image Viewer) has no access to the data that is internal to your application.

What you might have to do is create a Content Provider . http://web.archive.org/web/20111020204554/http://www.marcofaion.it/?p=7

Android Manifest.xml

<provider android:authorities="com.example.denandroidapp" android:enabled="true" android:exported="true" android:name=<fully classified name of provider class>>
</provider>

Creating Intent

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

Uri uri = Uri.parse("content://com.example.denandroidapp/" + filename);
intent.setDataAndType(uri, "image/jpeg");
like image 32
3tz Avatar answered Nov 05 '22 04:11

3tz


If a file is associated with your app (stored on the internal storage of your app space ), other apps can not access your file directly provided a valid file path. Instead, you have to create a file provider and generate a content uri.

First, add the file provider in your AndroidManifest.xml

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mydomain.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

Then you need to create an a file named file_paths in xml/file_paths.xml (the directory xml is not created by default, so create it).

file_paths.xml looks like

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="myFiles" path="./"/>
</paths>

add as much as paths you want your provider to access in .

atlast you need to create your intent

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
File imagePath = new File(context.getFilesDir(), "fileName");
Uri contentUri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", imagePath);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"image/*");
context.startActivity(intent);

Note: make sure the file path sepecefied in file_paths.xml and new File(context.getFilesDir(),"fileName"), matches. getFilesDir() will give you the root directory of your app.

like image 3
Nathaniel Avatar answered Nov 05 '22 05:11

Nathaniel