如何使用 Image RealPath 在 Android 编辑器中打开所选图像

How to Open Selected Image in Android Editor Using Image RealPath

我想在我的编辑器中打开 android 设备中的任何图像。当我们从设备 select 图片时,它应该会自动在照片编辑器 (adobe creativesdk) 中打开。以下是我的代码:

public class MainActivity extends AppCompatActivity {

    private ImageView mEditedImageView;
    Button openPhotoEditor, pickImage;
    String image = "content://media/external/images/media/####";
    TextView textView;

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

        Intent cdsIntent = AdobeImageIntent.createCdsInitIntent(getBaseContext(), "CDS");
        startService(cdsIntent);
        textView = (TextView) findViewById(R.id.textView);
        pickImage = (Button) findViewById(R.id.pick_image);
        pickImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
            }
        });

        mEditedImageView = (ImageView) findViewById(R.id.editedImageView);
        openPhotoEditor = (Button) findViewById(R.id.open_adobe_creative_sdk_editor);
        openPhotoEditor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri imageUri = Uri.parse(String.valueOf(image));
                //Uri uriFromPath = Uri.fromFile(new File(imageRes));

    // 2) Create a new Intent 
                Intent imageEditorIntent = new AdobeImageIntent.Builder(MainActivity.this)
                        .setData(imageUri)
                        .withOutputFormat(Bitmap.CompressFormat.JPEG) // output format
                        .withOutputSize(MegaPixels.Mp5) // output size
                        .withOutputQuality(100) // output quality
                        .build();

    // 3) Start the Image Editor with request code 1 
                startActivityForResult(imageEditorIntent, 1);
            }
        });
    }


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

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

                //4) Make a case for the request code we passed to startActivityForResult()
                case 1:

                    // 5) Show the image!
                    Uri editedImageUri = data.getParcelableExtra(AdobeImageIntent.EXTRA_OUTPUT_URI);
                    mEditedImageView.setImageURI(editedImageUri);

                    //Log the image URI in the Android Studio console
                    //Log.d("URI!", editedImageUri.toString());
                    break;
            }
        }

            if (requestCode == Activity.RESULT_OK && data != null) {
                String realPath;
                // SDK < API11
                if (Build.VERSION.SDK_INT < 11)
                    realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());

                    // SDK >= 11 && SDK < 19
                else if (Build.VERSION.SDK_INT < 19)
                    realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());

                    // SDK > 19 (Android 4.4)
                else
                    realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
            }

    }

}

和 RealPathUtil class

public class RealPathUtil { @SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
    String filePath = "";
    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            column, sel, new String[]{ id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return filePath;
}
...
...

}

这样解决:

public class MainActivity extends AppCompatActivity {

    private ImageView mEditedImageView;
    Button pickImage;

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

        mEditedImageView = (ImageView) findViewById(R.id.editedImageView);
        pickImage = (Button) findViewById(R.id.pick_image);


        pickImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Open the image source chooser immediately
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, 0);

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent 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 AdobeImageIntent.Builder(this)
                            .setData(selectedImageUri)
                            .build();
                    startActivityForResult(imageEditorIntent, 1);
                    break;

                // Attach the edited image to the ImageView
                case 1:
                    Uri editedImageUri = data.getParcelableExtra(AdobeImageIntent.EXTRA_OUTPUT_URI);
                    mEditedImageView.setImageURI(editedImageUri);

                    break;
                //4) Make a case for the request code we passed to startActivityForResult()

            }
        }
    }


}

this

的帮助下