2014-03-05 4 views
2

Android 용 간단한 이미지 갤러리 앱을 만들고 있습니다. SMS, 전자 메일, 채팅 응용 프로그램에 이미지를 추가 할 때 사용자가 이미지를 선택할 수있게하려는 경우 ... 내 응용 프로그램을 응용 프로그램 선택기에 표시하기 위해 의도 필터를 사용했습니다. 사용자가 이미지를 추가하고 싶은 메뉴 :Android - 의도에 대한 응답으로 이미지를 다른 응용 프로그램에 전달

 <intent-filter> 
      <action android:name="android.intent.action.GET_CONTENT" /> 
      <category android:name="android.intent.category.OPENABLE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*" /> 
      <data android:mimeType="vnd.android.cursor.dir/image" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.PICK" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*" /> 
      <data android:mimeType="vnd.android.cursor.dir/image" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.media.action.IMAGE_CAPTURE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

그런 다음 사용자가 이미지를 선택할 수 있습니다,하지만 난 발신자 응용 프로그램에 응답하는 가장 좋은 방법을 찾고 있어요.

나는 임시 파일로 이미지를 저장하고이 작동 해당 파일

public void respondWithImage(Bitmap bitmap){ 
    try { 
     //If needed create temp folder 
     File folder = new File(Environment.getExternalStorageDirectory() + "/temp/"); 
     if (!folder.exists()) { 
      folder.mkdirs(); 
     } 
     File nomediaFile = new File(folder, ".nomedia"); 
     if (!nomediaFile.exists()) { 
      nomediaFile.createNewFile(); 
     } 

     //Create temp image file 
     FileOutputStream out = new FileOutputStream(
       Environment.getExternalStorageDirectory() 
         + "/temp/temp.png"); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
     File bitmapFile = new File(
       Environment.getExternalStorageDirectory() 
         + "/temp/temp.png"); 

     //Tell parent activity to respond with the image 
     if (bitmapFile.exists()) { 
      Intent localIntent = new Intent().setData(Uri 
        .fromFile(bitmapFile)); 
      parentActivity.setResult(Activity.RESULT_OK, localIntent); 
     } else { 
      parentActivity.setResult(Activity.RESULT_CANCELED); 
     } 
     parentActivity.finish(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.d("error", "Error writing data"); 
    } 
} 

의 URI를 보내려고 노력했지만 나는 전화가 더 SDCARD가없는 경우는 어떻게 할 것인지 궁금합니다. 그 옵션을 시도 할 수 없다. 분명히 ext 카드로 자신의 메모리를 고려한 GS3을 가지고 있기 때문이다. (내가 틀렸다면 나에게 맞춰라.)

드로어 블을 가리키는 URI를 다시 보내려고했다. 내 애플 내 리소스 (PNG 파일) (내 애플 리케이션 리소스에 이미지가 문제가되지 않을)하지만 그것은 호출 응용 프로그램 (MMS) 충돌하게하고 디버거에서 오류를 catch하지 않습니다.

올바른 방법은 무엇입니까?

+0

다른 앱에 리소스를 전달할 수 없습니다. –

+0

를 하나의 필터에 모두 넣었습니까? 첫 번째 것도 두 번째 것을 다루고 있지 않습니까? – Eftekhari

답변

2

이 작동하지만 전화가 당신은 "sdcard에"를 사용하지 않는

더 SDCARD가없는 경우는 어떻게 할 것인지 궁금합니다.

내가 명백하게 내선 카드 (I 틀렸다면 정정 해줘) 일반적으로

외부 저장 장치로 자신의 메모리를 고려한 GS3을 가지고 있기 때문에 그 옵션을 시도 할 수는 온 - 보드 플래시, 일반적으로 내부 스토리지가있는 파티션과 동일합니다. the documentation for external storage은 좋지 않지만 검토해 볼 수 있습니다.

나는 또한 내 응용 프로그램 내에서 그릴 수 자원 (PNG 파일)를 가리키는 URI를 다시 보내려고했다 ...하지만 호출하는 응용 프로그램 (MMS) 충돌을하고 나는 오류를 잡을 수 없습니다 디버거에서.

타사 앱이 아닌 앱을 디버깅 할 때 디버거의 일부 타사 앱에서 충돌을 감지하지 않습니다. LogCat에는 스택 추적이 포함될 수 있습니다.

올바른 방법은 무엇입니까?

Use FileProvider, 혹은 my StreamProvider, 당신의 이미지를 제공하기위한 ContentProvider을 제공합니다. 그러면 Uricontent://Uri이됩니다. 이미지는 일반적으로 콘텐츠 제공 업체 (예 : 이메일 첨부 파일)에서 제공되므로 대부분의 앱에서 지원해야합니다.

관련 문제