簡體   English   中英

從活動中獲取意圖

[英]Receiving intent from activity

我有一個問題。 我有自己的文件瀏覽器,我想實現用於應用程序內文件打開的功能(即圖像,視頻,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();
    }
}

我的問題是,如何進行查看指定文件的活動以及如何獲取意圖? 謝謝。 請使用代碼作為答案,如果有任何明顯的變化,請編寫它們。 謝謝 :)

請在清單中添加

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM