2014-11-09 5 views
1

Я пытаюсь открыть файл jpg из папки res/raw с помощью кнопки в действии. Однако при запуске и тестировании приложения, я получаю сообщение об ошибке сказав:Как открыть ресурс jpg с помощью кнопки в действии

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 }` 

Это мой код:

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; 
} 

Я пропускаю что-то здесь :-)?

EDIT

я сейчас сделал это, но теперь при запуске приложения для проверки я получаю сообщение о том, «не удалось найти пункт» от нажатия кнопки. Вот мой код:

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); 
} 
+0

http://stackoverflow.com/questions/12585747/how-to- open-a-file-in-android-to-intent – sider

ответ

0

Я думаю, что вы забыли добавить file:/ при создании 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); 

Посмотрите здесь: No Activity found to handle Intent : android.intent.action.VIEW

+0

Спасибо, но я все равно получаю ту же ошибку. Я также пробовал ссылку, приведенную выше, но все же давал ту же ошибку. – Allrounder

0

Там нет оснований использовать File здесь. Просто введите Uri в намерение:

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);  
    } 

Что не так с решением?

  • слишком сложна (нет необходимости использовать файл)
  • MIMETYPE не является неправильным: использовать image/jpeg вместо application/jpg
+0

Спасибо, но отредактировал мой qeustion, теперь кажется, что он не может найти элемент/файл, который я думаю. – Allrounder

+0

[Stackoverflow - не форум] (http://meta.stackexchange.com/questions/92107/is-stack-overflow-a-forum/#92110): один точный вопрос -> ответы. Если вы столкнулись с другой проблемой: задайте другой вопрос и прочитайте [this] (http://stackoverflow.com/tour) и [this] (http://codeblog.jonskeet.uk/2010/08/29/writing- the-perfect-question /) перед публикацией – ben75

Смежные вопросы