调用OnStartActivityResult后,应用程序不会返回

时间:2017-02-25 07:10:52

标签: android image android-intent onactivityresult

我有一个活动,我用它来调用Intent从图库中选择图像。 选择图像后直接返回主屏幕,图库意图尝试返回我认为未找到的活动。

public void startScan(View view) { 

    Intent intent = new     Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONT    ENT_URI);
    startActivityForResult(intent, 99);
    }

Mainfest:

<application
tools:replace="android:icon,android:theme"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:persistent="true"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<activity
    android:name=".MainActivity"
    android:label="מערכת דיווחים"
    android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustResize|stateAlwaysVisible|stateVisible|adjust    Pan">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".LoginActivity"
          android:windowSoftInputMode="adjustResize|stateAlwaysVisible|stateVisible|adjust    Pan" >
</activity>
</application>

OnActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent      data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 99 && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),     uri);
            getContentResolver().delete(uri, null, null);
           pushbutton.setText(bitmap.getByteCount());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }

1 个答案:

答案 0 :(得分:0)

在onActivityResult中尝试此代码:

Uri originalUri = data.getData();
                final int takeFlags = data.getFlags()
                        & (Intent.FLAG_GRANT_READ_URI_PERMISSION |      Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
String selectedImagePath = getPath(context,originalUri );

以下是getPath(context,Uri)

 public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

希望它会对你有所帮助。