Gopi
Gopi

Reputation: 1

Adding images to a folder in SDCARD

Everytime the image is captured the folder creation section works fine but the image is not added to the folder

photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) 
{
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode != RESULT_CANCELED) 
{
folder = new File(Environment.getExternalStorageDirectory() + File.separator+"folder/");                          

if(!folder.exists())

        {
            folder.mkdirs();
            Log.d("SDcard", "Folder created");
        }
        else
        {
            Log.d("SDCard", "Folder already exists");
        }
File file = new File(Environment.getExternalStorageDirectory() +        File.separator +"folder/");
        Uri photoPath = Uri.fromFile(file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);    ` 
        }
        }

If I add the folder creation and save image logic to the onclick() directly it shows error stating "Failure delivering result info"

*Please do help *

Upvotes: 0

Views: 48

Answers (2)

msevgi
msevgi

Reputation: 4904

public void saveBitmapToFile(Bitmap bmp) {

        File mAppBaseDir;
        if (isExternalStorageWritable())
            mAppBaseDir = new File(Environment.getExternalStorageDirectory(), "FolderName");
        else
            mAppBaseDir = new File(getApplicationContext().getFilesDir().getParent()).getAbsoluteFile();

        if (!mAppBaseDir.exists()) {
            mAppBaseDir.mkdirs();
        }
        File imageDir = new File(mAppBaseDir, "Profile");
        if (!imageDir.exists())
            imageDir.mkdirs();
        File file = new File(imageDir + "/" + "profile.png");
        if (file.exists()) {
            file.delete();
        }
        try {
            writeBytesToFile(file, bitmapToByte(bmp));

        } catch (IOException e) {
            // show alert for retry choose photo
            e.printStackTrace();
        }
    }



public void writeBytesToFile(File file, byte[] bytes) throws IOException {
        BufferedOutputStream bos = null;

        try {
            FileOutputStream fos = new FileOutputStream(file.getPath());
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            Log.e("", e.getMessage());
        } finally {
            if (bos != null) {
                try {
                    bos.flush();
                    bos.close();
                } catch (Exception e) {
                    Log.e("", e.getMessage());
                }
            }
        }
    }

public byte[] bitmapToByte(Bitmap bitmapFinally) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmapFinally.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }

Upvotes: 1

Randyka Yudhistira
Randyka Yudhistira

Reputation: 3652

I answer this question based on your comment "can you please elaborate". CMIIW :

photoButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        folder = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/");

        if (!folder.exists())

        {
            folder.mkdirs();
            Log.d("SDcard", "Folder created");
        } else {
            Log.d("SDCard", "Folder already exists");
        }
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/");
        Uri photoPath = Uri.fromFile(file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
});

"Ive declared the cameraIntent object before oncreate() method"
that's no problem about that, but onActivityResult will be triggered after you call startActivityForResult, then the intent wont have the extra you give

Upvotes: 0

Related Questions