1

I have a button, when pressing that I'll get an alertDialog to choose a pick from: Gallery or take a picture. When pressing take a picture and take the picture, the picture isn't being saved in the gallery. Why is this? I need the picture because later I'm planning to send it with a post to upload it to my server and add the string to the DB. Can someone tell me what I'm doing wrong?

private void selectImage() {
        final CharSequence[] items = { "Take Photo", "Choose from gallery",
                "Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(AddBuilding.this);
        builder.setTitle("Add Photo!");

        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment
                            .getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, REQUEST_CAMERA);
                }

                else if (items[item].equals("Choose from gallery")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);
                }

                else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File f = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        btmapOptions);

                bm = Bitmap.createScaledBitmap(bm, 400, 300, true);
                btnImg.setImageBitmap(bm);
                btnImg.setScaleType(ScaleType.CENTER_INSIDE);

                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream fOut = null;
                File file = new File(path, String.valueOf(System
                        .currentTimeMillis()) + ".jpg");
                try {
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE) {
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, AddBuilding.this);
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            bm = Bitmap.createScaledBitmap(bm, 400, 300, true);
            btnImg.setImageBitmap(bm);
            btnImg.setScaleType(ScaleType.CENTER_INSIDE);
        }
    }
}



public String getPath(Uri uri, Activity activity) {
        String[] projection = { MediaColumns.DATA };
        Cursor cursor = activity
                .managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
mXX
  • 3,595
  • 12
  • 44
  • 61

2 Answers2

2

You are getting External storage. Instead save your image with this:

MediaStore.Images.Media.insertImage(getContentResolver(), 
                                    Bitmap, 
                                    Title, 
                                    Description);

This will save the image in gallery. See insertImage().

Parameters
cr The content resolver to use image
Path The path to the image to insert
name The name of the image
description The description of the image

Returns
The URL to the newly created image

Community
  • 1
  • 1
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • Where exactly should I do this? `File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");` thought replacing this, but can't find `MediaStore` – mXX Jun 17 '13 at 21:38
0

Its late but posting the answer for others. Do this

File f = new File (Environment.getExternalStorageDirectory() + "/CameraImages/temp.jpg");

user3507451
  • 39
  • 1
  • 10