Сохранение изображений в файл android

Мне нужна помощь в моей проблеме сохранения изображения. Я использую Adobe Photo SDK для редактирования своего изображения. Ссылка на документацию(https://creativesdk.adobe.com/docs/android/#/articles/imageediting/index.html). Они говорят, что используйте .withOutput(Uri) для сохранения изображения, которое я создал, но получаю мое изображение с ошибкой.

public class MainActivity extends AppCompatActivity {
    private static final int IMG_CODE_EDIT = 263;
    private Button mPickbtn;
    private Button mCapturebtn;
    private static int RESULT_LOAD_IMAGE = 1;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private String mCurrentPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPickbtn = (Button) findViewById(R.id.pick_image);
        mCapturebtn = (Button) findViewById(R.id.button2);

        Intent cdsIntent = AdobeImageIntent.createCdsInitIntent(getBaseContext(), "CDS");
        startService(cdsIntent);

    }

    public void takePicture(View view) {
        // create Intent to take a picture and return control to the calling application
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // start the image capture Intent
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    public void onClick(View view) {

        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

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

        //Gets the gallery image uri
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            editPic(selectedImage);
        } 

        //picture from camera
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Uri mPicTaken = data.getData();
                editPic(mPicTaken);
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }

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

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

                    /* 5) Show the image! */
                    Uri editedImageUri = data.getData();
                    Intent intent = new Intent("com.ayyogames.photoapp.Share");
                    intent.putExtra("imageUri", editedImageUri);
                    startActivity(intent);

                    break;
            }
        }

    }

public void editPic(Uri uri) {

        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.ayyogames.photoapp.fileprovider",
                    photoFile);

            Intent intent = new AdobeImageIntent.Builder(this)
                    .setData(uri)
                    .withOutputSize(MegaPixels.Mp10)
                    .withOutputQuality(100)
                    .withOutput(photoURI)
                    .build();

            startActivityForResult(intent, IMG_CODE_EDIT);
        }
    }


private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

Поделиться классом

public class Share extends AppCompatActivity {

    private ImageView mEditedImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        mEditedImageView = (ImageView) findViewById(R.id.editedImageView);

        Intent intent = getIntent();
        Uri uri = intent.getParcelableExtra("imageUri");

        mEditedImageView.setImageURI(uri);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu,menu);

        return true;
    }
}

person S12    schedule 02.07.2016    source источник


Ответы (1)


В руководстве разработчика редактора изображений в настоящее время указано, что вы должен передать Uri в withOutput(), но это неверно.

Попробуйте передать File:

    /* 1) Change the argument to your desired location */
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
            .setData(imageUri)
            .withOutput(file) /* 2) Pass the File here */
            .build();

Ваш аргумент конструктору File должен быть изменен на любое место, которое вы хотите.

person Ash Ryan Arnwine    schedule 11.07.2016