1

I am trying to open a jpg file from res/raw folder using a button in the activity. However when launching and testing the app I get an error saying:

11-09 10:20:51.654: E/AndroidRuntime(3247): android.content.ActivityNotFoundException: 
No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android.resource:/com.image.imagetest/2131034112 typ=application/jpg flg=0x4000000 }`  

This is my code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_image, container, false);

    Button button = (Button) rootView.findViewById(R.id.Image1);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(final View v) {

            File jpgFile = new File("android.resource://com.image.imagetest/" + R.raw.mainimage);
            Uri path = Uri.fromFile(jpgFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setDataAndType(path, "application/jpg");

            startActivity(intent);    

        }
    });
    return rootView;
}

Am I missing something here :-) ?

EDIT

I have now done this but now when launching app to test i get a message saying "unable to find item" from clicking the button. Here is my code:

public void onClick(final View v) {
    File jpgFile = new File("android.resource://com.image.imagetest/" + R.raw.mainimage);
    Uri path = Uri.fromFile(jpgFile);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "image/jpeg");
    startActivity(intent);  
}
sam
  • 2,780
  • 1
  • 17
  • 30
Allrounder
  • 685
  • 2
  • 9
  • 20
  • http://stackoverflow.com/questions/12585747/how-to-open-a-file-in-android-via-an-intent – sider Nov 09 '14 at 08:33

2 Answers2

0

I think you forgot to add file:/ when creating intent :

File jpgFile = new File("file:/android.resource://com.image.imagetest/" + R.raw.mainimage);
Uri path = Uri.fromFile(jpgFile);
Intent intent = new Intent(Intent.ACTION_VIEW);

Take a look here : No Activity found to handle Intent : android.intent.action.VIEW

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • Thanks, but am still getting the same error. I have tried the link given above aswell but still giving the same error. – Allrounder Nov 09 '14 at 09:07
0

There is no reason to use a File here. Just put the Uri in the Intent :

public void onClick(final View v) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("android.resource://com.image.imagetest/" + R.raw.mainimage));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);    
  }

What's wrong with your solution ?

  • too complex (no need to use a File)
  • the mimetype is wrong : use image/jpeg instead of application/jpg
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Thank you, but have edited my qeustion, it seems now that it can't locate the item/file i think. – Allrounder Nov 09 '14 at 10:20
  • [Stackoverflow is not a forum](http://meta.stackexchange.com/questions/92107/is-stack-overflow-a-forum/#92110) : one precise question --> answers. If you are stuck with another problem : ask another question and read [this](http://stackoverflow.com/tour) and [this](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) before posting – ben75 Nov 09 '14 at 10:44