将图像保存到文件android

时间:2016-07-02 03:40:46

标签: android file start-activity adobecreativesdk

我在保存图片方面需要帮助。我正在使用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;
    }
}

1 个答案:

答案 0 :(得分:0)

目前Image Editor developer guide表示您应该将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构造函数的参数应该更改为您想要的任何位置。