2014-09-15 2 views
0

하나의 문제점이 있습니다. 난 내 자신의 파일 탐색기를 내가 (그래서, 즉 이미지, 동영상, PDF 파일 등)에-응용 프로그램 파일 열기에 대한 기능을 구현하려는 이것은활동의 의도 수신

public static void openFile(final Context context, final File target) { 
    final String mime = MimeTypes.getMimeType(target); 
    final Intent i = new Intent(Intent.ACTION_VIEW); 

    if (mime != null) { 
     i.setDataAndType(Uri.fromFile(target), mime); 
    } else { 
     i.setDataAndType(Uri.fromFile(target), "*/*"); 
    } 

    if (context.getPackageManager().queryIntentActivities(i, 0).isEmpty()) { 
     Toast.makeText(context, R.string.cantopenfile, Toast.LENGTH_SHORT) 
       .show(); 
     return; 
    } 

    try { 
     context.startActivity(i); 
    } catch (Exception e) { 
     Toast.makeText(context, 
       context.getString(R.string.cantopenfile) + e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

내 문제는 내 코드입니다, 어떻게 활동을 할 수 있습니다 지정된 파일을보고 그것에 인 텐트를받는 방법? 감사합니다. 답변으로 코드를 사용하십시오. 명시적인 변경 사항이있는 경우 코드를 작성하십시오. 감사합니다 :)

+0

이 추가하십시오. 이 가이드 참조 : https://developer.android.com/guide/components/intents-filters.html – Karakuri

+0

onActivityResult를 사용해야한다고 생각합니다. http://developer.android.com/reference/android/app/Activity.html –

+0

글쎄, 나는 이것에 처음이지만, 내가 원하는 것을 찾지 못했습니다. 내 질문은 특정 파일을 열려면 해당 ViewerActivity 작성하는 방법을했습니다 – Trabefi

답변

3

매니페스트

<activity android:name=".ui.MyActivity" > 
    <intent-filter> 
     <action android:name="android.intent.action.SEND" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="image/*" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.SEND" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="text/plain" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.SEND_MULTIPLE" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="image/*" /> 
    </intent-filter> 
</activity> 

에 그리고 당신의 코드 인 텐트 필터는 당신이 원하는

void onCreate (Bundle savedInstanceState) { 
    ... 
    // Get intent, action and MIME type 
    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 

    if (Intent.ACTION_SEND.equals(action) && type != null) { 
     if ("text/plain".equals(type)) { 
      handleSendText(intent); // Handle text being sent 
     } else if (type.startsWith("image/")) { 
      handleSendImage(intent); // Handle single image being sent 
     } 
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { 
     if (type.startsWith("image/")) { 
      handleSendMultipleImages(intent); // Handle multiple images being sent 
     } 
    } else { 
     // Handle other intents, such as being started from the home screen 
    } 
    ... 
} 

void handleSendText(Intent intent) { 
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); 
    if (sharedText != null) { 
     // Update UI to reflect text being shared 
    } 
} 

void handleSendImage(Intent intent) { 
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    if (imageUri != null) { 
     // Update UI to reflect image being shared 
    } 
} 

void handleSendMultipleImages(Intent intent) { 
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); 
    if (imageUris != null) { 
     // Update UI to reflect multiple images being shared 
    } 
} 
+0

난 뷰어 응용 프로그램에서 의도를 처리하는 방법을 알고 싶었 – Trabefi

+0

@ Traabefi 뷰어 응용 프로그램? 무슨 말이야?? – sam

+0

내 파일 탐색기가 있습니다. 그리고 다양한 파일을 열 수있는 활동을하고 있습니다. 그리고 거기에 의도를 처리하는 방법을 알고 싶었습니다 – Trabefi