0

I m selecting Image From Gallery and crop in My Phone And Uploading To Server ..

When i m testing Someother devices then image was Unable Picked to some devices

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);
}

here Is My Code please Tell ME Where I m Doing Wrong

I want To Remove To Create An New File On Sd Card if Sd Card Is Not Available on no space app Crashed

user3825086
  • 111
  • 1
  • 2
  • 12
  • 4
    possible duplicate of [How to pick an image from gallery (SD Card) for my app?](http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app) – Tobrun Feb 18 '15 at 13:43
  • This is what happens when you copy/paste code from outdated/incomplete tutorials without understanding what the code does... – 2Dee Feb 18 '15 at 13:44
  • @2Dee ..I m new On Android tutorial was to select image from gallery and upload to server – user3825086 Feb 18 '15 at 13:48

1 Answers1

0

//Initialize variable

int select_photo = 1;

//Make this method to start intent

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

//Now on 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);

            }
        }
    }

//for checking sdcard is present or not you can use

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

else
//not present

and finally call the starting method. Hope it helps.

Surender Kumar
  • 1,123
  • 8
  • 15