在Genymotion中将位图保存为图像

时间:2017-05-29 10:34:06

标签: android bitmap genymotion

我知道之前可能会问这个问题,我尝试了这里给出的所有答案,但它没有用。 我从图库中选择一个图像,显示它,然后更改一些像素并尝试将其保存在SD卡中,但它没有保存。  布局:

<Button
    android:id="@+id/pick_image_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Image" />

<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

.java文件

Button buttonLoadImage = (Button) 
rootView.findViewById(R.id.pick_image_button);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

   Intent i = new Intent(Intent.ACTION_PICK);
   File pictureDirectory= 
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);
   pictureDirectoryPath=pictureDirectory.getPath();

   Uri data=Uri.parse(pictureDirectoryPath);
   i.setDataAndType(data,"image/*");
   startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK
           ) {

        try{
         imageUri = data.getData();
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = 
              getActivity().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
            Utility.showCustomAlert(picturePath,getActivity(),"error");


        imageView = (ImageView) rootView.findViewById(R.id.image_view);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

   try{
       inputstream = 
           getContext().getContentResolver().openInputStream(imageUri);
       Bitmap bmp = BitmapFactory.decodeStream(inputstream);
        imageView.setImageBitmap(bmp);
      }
   catch (Exception e)
    {
           e.printStackTrace();
     }
}

保存图片的代码(不是完整代码,而是保存图像的部分)

 String sdCardDirectory = 
 Environment.getExternalStorageDirectory().getAbsolutePath();
 File dir = new File(sdCardDirectory+ "/download");
 dir.mkdirs();
 String fname="imazh.png";
 File file=new File(dir,fname);
 if(file.exists())file.delete();


 // Encode the file as a PNG image.
 FileOutputStream outStream;

      outStream = new FileOutputStream(file);
      bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
      outStream.flush();
      outStream.close();
      success = true;
      MediaScannerConnection.scanFile(getContext(),
                    new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {

                        }
                    });

我已在清单文件中添加了权限

0 个答案:

没有答案