1

当我选择图像时,我在没有任何通知的情况下阻止了我的应用程序退出,但是当我选择论坛照片或画廊时没有崩溃。我creativesdk用于照片编辑。

截图

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();


    }


}

我该如何解决?

4

3 回答 3

1

问题的来源

您可以稍微简化您的switch语句并直接传递给'方法。case 0selectedImageUriAviaryIntentsetData()

因此,您将不需要您的getPath()辅助方法。

示例代码

以下是基于您上面的代码的工作代码,它将:

  1. 立即打开图像源选择器
  2. 在 Creative SDK 图像编辑器中打开所选图像
  3. 将编辑后的图像附加到ImageView

请参阅示例代码中的注释以查找这些事情发生的位置。

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

    }

    // ...
}
于 2016-01-18T16:10:11.207 回答
1

尝试在清单文件中的 Activity 上将 true 更改为 false。

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

于 2017-05-24T16:12:18.667 回答
0

你的代码:galleryIntent.setAction(Intent.ACTION_GET_CONTENT)

使用这个: Intent GalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

于 2016-05-12T07:58:35.717 回答