android app exit when select image

2019-08-13 09:40发布

问题:

I am blocked my app exit without any notification when I select images but when I select forum photo or gallery did not crashed. I use creativesdk for photo edited.

screnshoot

package com.lamba.selfie;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.aviary.android.feather.sdk.AviaryIntent;

public class MainActivity extends AppCompatActivity {
    private ImageView mResultImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mResultImageView = (ImageView) findViewById(R.id.resultImageView);


        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), 0);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    Uri selectedImageUri = data.getData();
                    Uri imageUri = Uri.parse(getPath(selectedImageUri));
                    Intent imageEditorIntent = new AviaryIntent.Builder(this)
                            .setData(imageUri)
                            .build();
                    startActivity(imageEditorIntent);
                /*case 1:
                    startActivityForResult(imageEditorIntent, 1);
                    Uri mImageUri = data.getData();
                    mResultImageView.setImageURI(mImageUri);
                    break;*/
            }
        }
    }

    public String getPath(Uri uri) {
        // just some safety built in
        if (uri == null) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();


    }


}

How could I fix it?

回答1:

Source of the issue

You can simplify your switch statement's case 0 a bit and pass selectedImageUri directly to the AviaryIntent's setData() method.

As a result, you won't need your getPath() helper method.

Example code

Below is working code, based on your code above, that will:

  1. Open the image source chooser immediately
  2. Open the selected image in the Creative SDK Image Editor
  3. Attach the edited image to the ImageView

See the comments in the example code to find where these things happen.

public class MainActivity extends AppCompatActivity {

    private ImageView mSelectedImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSelectedImageView = (ImageView) findViewById(R.id.selectedImageView);

        // Open the image source chooser immediately
        Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(galleryIntent, "Select an image"), 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {

                // Open the Creative SDK Image Editor with the chosen image
                case 0:
                    Uri selectedImageUri = data.getData();
                    Intent imageEditorIntent = new AviaryIntent.Builder(this)
                            .setData(selectedImageUri)
                            .build();

                    startActivityForResult(imageEditorIntent, 1);

                    break;


                // Attach the edited image to the ImageView
                case 1:
                    Uri editedImageUri = data.getData();
                    mSelectedImageView.setImageURI(editedImageUri);

                    break;
            }
        }

    }

    // ...
}


回答2:

try to change true to false on your Activity in the manifest file.

android:noHistory="true" to android:noHistory="false"



回答3:

your code : galleryIntent.setAction(Intent.ACTION_GET_CONTENT)

Use this : Intent GalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);