如何从图库中选择图像

时间:2015-02-18 13:39:05

标签: android onactivityresult

我从图库中选择图像并在我的手机中裁剪并上传到服务器..

当我测试其他设备时 图像无法选择某些设备

public void selectImageFromGallery() {
    String TEMP_PHOTO_FILE = "temporary_holder.jpg";  

     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     photoPickerIntent.setType("image/*");
     photoPickerIntent.putExtra("crop", "true");
     photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
     photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
     startActivityForResult(photoPickerIntent, PICK_IMAGE);


}

private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
}

private File getTempFile() {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        File file = new File(Environment.getExternalStorageDirectory(),"temporary_holder.jpg");
        try {
            file.createNewFile();
        } catch (IOException e) {}

        return file;
    } else {

        return null;
    }
}

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

        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode)
        {
            case PICK_IMAGE:
                if (resultCode == RESULT_OK) {  
                    if (imageReturnedIntent!=null) {

                        File tempFile = getTempFile();

                        String filePath= Environment.getExternalStorageDirectory()
                            +"/"+"temporary_holder.jpg";
                        System.out.println("path "+filePath);


                        decodeFile(filePath);

                        if (tempFile.exists()) tempFile.delete();
                    }
                }
        }
        } 

/**
 * The method decodes the image file to avoid out of memory issues. Sets the
 * selected image in to the ImageView.
 * 
 * @param filePath
 */
public void decodeFile(String filePath) {
 // Decode image size
 BitmapFactory.Options o = new BitmapFactory.Options();
 o.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(filePath, o);

 // The new size we want to scale to
 final int REQUIRED_SIZE = 1024;

 // Find the correct scale value. It should be the power of 2.
 int width_tmp = o.outWidth, height_tmp = o.outHeight;
 int scale = 1;
 while (true) {
  if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
   break;
  width_tmp /= 2;
  height_tmp /= 2;
  scale *= 2;
 }

 // Decode with inSampleSize
 BitmapFactory.Options o2 = new BitmapFactory.Options();
 o2.inSampleSize = scale;
 bitmap = BitmapFactory.decodeFile(filePath, o2);

 roundedImage=new RoundedImage(bitmap);
  image.setImageDrawable(roundedImage);
}

这是我的代码 请告诉我我做错了什么

如果Sd卡在没有空间的情况下不可用,我想删除在Sd卡上创建新文件 app崩溃了

1 个答案:

答案 0 :(得分:0)

//初始化变量

int select_photo = 1;

//使此方法启动意图

void Profile_Image_Pick() {
        Intent in = new Intent(Intent.ACTION_PICK);
        in.setType("image/*");
        startActivityForResult(in, select_photo);
    }

//现在在ActivityforResult上

@Override
    protected void onActivityResult(int requestCode, int resultCode,

        super.onActivityResult(requestCode, resultCode, imagereturnintent);
        switch (requestCode) {
        case select_photo:
            if (resultCode == RESULT_OK) {

                final Uri imageuri = imagereturnintent.getData();


            final InputStream imageStream = getContentResolver()
                    .openInputStream(imageUri);
            bitmap = BitmapFactory.decodeStream(imageStream);


            Bitmap bt = Bitmap.createScaledBitmap(bitmap, 70, 70, false);
            profile_image.setImageBitmap(bt);

            }
        }
    }

//检查sdcard是否存在,您可以使用

if(android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED))
//sdcard present

else
//not present

最后调用启动方法。 希望它有所帮助。