Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:23:50+00:00 2026-06-16T05:23:50+00:00

I have an application in which an ImageView is set and can be clicked

  • 0

I have an application in which an ImageView is set and can be clicked to be opened in the gallery.

By default, I use the following code to get a file directory from the external storage to store my jpegs:

File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");

But! Suppose the external storage is not mounted or simply does not exist (Galaxy Nexus), this doesn’t work. So I wrote an if-statement around it and get the internal cache dir as a fall back.

String state = Environment.getExternalStorageState()
if(Environment.MEDIA_MOUNTED.equals(state)){ 
    File picsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");    
}else{
    context.getCacheDir();
}

The images show up fine in the ImageView, but don’t come through when my intent launches.

Intent intent = new Intent();             
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgFile), "image/jpeg");
startActivity(intent);

The gallery gets loaded, but shows a black screen. Presumably because the gallery has no access to files in the cache dir of my app.

As an alternative, I tried using the media content provider that uses MediaStore.Images.Media.INTERNAL_CONTENT_URI, but this leads to an error when trying to inser the image:

java.lang.UnsupportedOperationException: Writing to internal storage is not supported.

What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:23:52+00:00Added an answer on June 16, 2026 at 5:23 am

    i suppose the problem here is that you are trying open with the gallery a file saved in a private space of memory (getCacheDir return a path relative to your application and only your application can access that memory path)

    If you can’t save in external memory, you can try to save in a public path (but that way your media files can be manipulated by every app and if you uninstall your application it doesn’t clean generated media that you saved there)

    If you want to use private internal memory, you can write your ContentProvider

    i edit to post a content provider i use to acomplish what i said.
    this is my content provider (i just posted the relevant part you need):

    public class MediaContentProvider extends ContentProvider {
    private static final String TAG = "MediaContentProvider";
    
    // name for the provider class
    public static final String AUTHORITY = "com.way.srl.HandyWay.contentproviders.media";
    
    private MediaData _mediaData;
    
    // UriMatcher used to match against incoming requests
    private UriMatcher _uriMatcher;
    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public boolean onCreate() {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
        // Add a URI to the matcher which will match against the form
        // 'content://com.stephendnicholas.gmailattach.provider/*'
        // and return 1 in the case that the incoming Uri matches this pattern
        _uriMatcher.addURI(AUTHORITY, "*", 1);
    
        return true;
    }
    
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    
        Log.v(TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment());
    
        // Check incoming Uri against the matcher
        switch (_uriMatcher.match(uri)) {
    
        // If it returns 1 - then it matches the Uri defined in onCreate
            case 1:
    
                // The desired file name is specified by the last segment of the
                // path
                // E.g.
                // 'content://com.stephendnicholas.gmailattach.provider/Test.txt'
                // Take this and build the path to the file
                // String fileLocation = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();
                Integer mediaID = Integer.valueOf(uri.getLastPathSegment());
    
                if (_mediaData == null) {
                    _mediaData = new MediaData();
                }
                Media m = _mediaData.get(mediaID);
    
                // Create & return a ParcelFileDescriptor pointing to the file
                // Note: I don't care what mode they ask for - they're only getting
                // read only
                ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(m.filePath), ParcelFileDescriptor.MODE_READ_ONLY);
                return pfd;
    
                // Otherwise unrecognised Uri
            default:
                Log.v(TAG, "Unsupported uri: '" + uri + "'.");
                throw new FileNotFoundException("Unsupported uri: " + uri.toString());
        }
    }
    

    then you need in the manifest the reference to your contentprovider, in my case it was

    <provider
            android:name=".contentproviders.MediaContentProvider"
            android:authorities="com.way.srl.HandyWay.contentproviders.media" >
        </provider>
    

    and then use it like this

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("content://" + MediaContentProvider.AUTHORITY + "/" + m.id), "image/jpg");
    

    in my case m is an entity that store an id that point to a sqlite db and i use a class that fetch data to populate again the object (with _mediaData), you can just change the code to fit your needs

    this way i solved exactly your problem in my application

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one ImageView in my application which can be situated anywhere on screen
I have a gallery which contains one textview and imageview. If i set OnClickListener
I have application which needs to use a dll (also written by me) which
I have an application which uses code that produces various types of objects and
I have an application which is packaged as a .war file. It has GWT
I have a application which handles picture chosen from gallery. But there is a
I have a Gallery view in my application which is working fine. When clicking
I have this application which animates two ImageViews ... I manage to use Menu
I have this layout in my application, the problem is, the ImageView which has
I have a class which is used to get media file thumbs. This Loader

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.