2

What i am trying to do is create a new folder and save the image captured by camera in that folder.I am trying to achieve it using following snipet:

public class DocsAdd extends Activity
{   
    private static int TAKE_PICTURE = 1;
    private Uri outputFileUri;
    Bitmap thumbnail;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        File filename;
        try 
        {
            long currentTime = System.currentTimeMillis();
            String fileName = "img" + currentTime+".jpg";
            String path = Environment.getExternalStorageDirectory().toString();             
            filename = new File(path + "/AutoistDiary/"+ fileName);
            FileOutputStream out = new FileOutputStream(filename);            
            out.flush();
            out.close();     
            MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(), filename.getName());

          outputFileUri = Uri.fromFile(filename);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, TAKE_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;            
            if (data!=null) 
            {
                if (data.hasExtra("data")) 
                {
                    thumbnail = data.getParcelableExtra("data");
                    Intent docsShow_intent=new Intent(this,DocsShow.class);
                    startActivity(docsShow_intent);
                }
            } 
            else 
            {
            }

        }  
    }
}

but when i run it on Optimus1 it is not returning any result to my activity, came across a link Camera on Android Example which states the same problem that i am facing currently so can help me with this. ?

Community
  • 1
  • 1
sankettt
  • 2,387
  • 4
  • 23
  • 31

1 Answers1

4

Unfortunately there is a bug on some devices causing the Intend data parameter in onActivityResult to be null when you use the MediaStore.EXTRA_OUTPUT flag in your intent for the camera. A workaround is to keep the outputFileUri variable global so that you can access it again in your onActivityResult method. Something like this:

Uri outputFileUri; //global variable

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo= new File(Environment.getExternalStorageDirectory(), "Autoist Diary"); 

if (! photo.exists())  
{  
    photo.mkdirs();  
    if (! photo.mkdirs())  
    {  
        Log.i("MyCameraApp", "failed to create directory");  
    }  
}  
puc_img=new File(photo,"Puc_Img.jpg");  
outputFileUri = Uri.fromFile(photo);  
intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);  
StartActivityForResult(intent, TAKE_PICTURE);  

public void onActivityResult(int requestCode, int resultCode, Intent data)  
{  
    super.onActivityResult(requestCode, resultCode, data);  
    switch (requestCode)   
    {  
       case TAKE_PICTURE:  
           if (resultCode == Activity.RESULT_OK)   
           {                   
               if(data != null) { //if the intent data is not null, use it
                  puc_image = getImagePath(Uri.parse(data.toURI())); //update the image path accordingly
                  StoreImage(this,Uri.parse(data.toURI()),puc_img);
               }
               else { //Use the outputFileUri global variable
                  StoreImage(this,outputFileUri,puc_img);
               }
           }   
       break;
    }  
}  
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir)  
{  
    Bitmap bm = null;  
    try   
    {  
        bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);  
        FileOutputStream out = new FileOutputStream(imageDir);  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, out);  
        bm.recycle();  
    }  
    catch (FileNotFoundException e)   
    {  
        e.printStackTrace();  
    }  
    catch (IOException e)   
    {  
       e.printStackTrace();  
    }  
    catch (Exception e)   
    {  
        e.printStackTrace();  
    }  
}
public String getImagePath(Uri uri) {
    String selectedImagePath;
    // 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        selectedImagePath = cursor.getString(column_index);
    } else {
        selectedImagePath = null;
    }

    if (selectedImagePath == null) {
        // 2:OI FILE Manager --- call method: uri.getPath()
        selectedImagePath = uri.getPath();
    }
    return selectedImagePath;
}

This workaroud has worked for me. Hope that helps.

AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • giving error at StoreImage(this,Uri.parse(outputFileUri),puc_img); Uri.parse() parses a string and not a Uri.so what i am supposed to pass to this method – sankettt Jul 23 '12 at 09:11
  • @sankettt Sorry my fault. I have updated my answer. You already the correct `Uri` for the `StoreImage` method. – AggelosK Jul 23 '12 at 09:46
  • The code is working properly but i am facing one strange issue..the file saved on the device i mentioned is 0kb.. what can be the issue with this. but at the same time when i tried on other device it is working properly. may be this is the bug of 2.2.. any help regarding this – sankettt Oct 22 '12 at 07:23
  • @sankettt I have updated my answer. Hope this solves your problem. I think the problem was that the variable `puc_image` was not updated in `onActivityResult` when the `Intent data` was not null. – AggelosK Oct 22 '12 at 07:49
  • hey @angelo i have used some other code which is working fine for 2.3 but stores 0kb for 2.2 i am putting the code here for u to refer.! – sankettt Oct 22 '12 at 08:31
  • @sankettt I dont think the problem is the version of the Adnroid but the diffferent devices. Every device has its own implementation for the camera, and some of them have the problem mentioned in your question. I am not sure the code you are using is absolutely correct. I think it only works in devices that do not have the `Intent data` null bug in `onActivityResult` method. I recommend you to make the appropriate modifications with the code snipet i posted. – AggelosK Oct 22 '12 at 08:56
  • ohk will check it. if any problem i would get back to u. – sankettt Oct 22 '12 at 09:03
  • can u help me with another problem regarding animation i have a question that i have posted can you check it ? http://stackoverflow.com/questions/13006157/animation-not-repeating-on-click-of-image#comment17648861_13006157 – sankettt Oct 22 '12 at 09:04
  • @sankettt I will gladly help you (if i can ofcourse) with your question but it seems that you already found a solution to your question:) – AggelosK Oct 22 '12 at 09:27
  • Can't believe this still isn't fixed, just got this error. – basickarl Apr 03 '15 at 13:24