Question

Here is my camera code. I'm writing a function which use startActivityForResult to transact the result. But when I choose ACTION_IMAGE_CAPTURE, normally it will capture the pic and I can go and find this pic's path to use. However, I can't find the path in my mobile phone. It seems this code can't use in my phone and thus I can't save pic in my mobile phone.

My mobile phone model is SONY xperia C and SONY xperia L.

Here are my choose code

public void function()
{
    AlertDialog.Builder builder =  new AlertDialog.Builder(repair.this);
    builder.setTitle("Please select").setPositiveButton("camera", new DialogInterface.OnClickListener()
    {       
        public void onClick(DialogInterface dialog, int i) 
        {       
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 1);
        }       
    }).setNegativeButton("album",  new DialogInterface.OnClickListener() 
    {           
        public void onClick(DialogInterface dialog, int i) 
        {               
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, 2);               
        }
   }).show();
}

And here are my onActivityResult code.

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inSampleSize = calculateInSampleSize(options, 2048, 2048);
    options.inJustDecodeBounds = false;

    if (resultCode == RESULT_OK)
    {
        if (requestCode == 1 || requestCode == 2) 
        {
            Uri photoUri = data.getData();  
            String[] pojo = {MediaStore.Images.Media.DATA};
            Cursor cursor =  managedQuery(photoUri, pojo, null, null,null);     
            if(cursor != null )
            {
                int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
                cursor.moveToFirst();
                picPath = cursor.getString(columnIndex);
            }               
            if(picPath != null && ( picPath.endsWith(".png") || picPath.endsWith(".PNG") ||picPath.endsWith(".jpg") ||picPath.endsWith(".JPG")  ))
            {
                    picPath1 = picPath;
                    Bitmap bm = BitmapFactory.decodeFile(picPath1, options);
                    imgbtnphoto1.setImageBitmap(bm);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();                
                    bm.compress(CompressFormat.JPEG, 80, bos);
                    byte[] data1 = bos.toByteArray();
                    bab1 = new ByteArrayBody(data1, "img1.jpg");
            }
            else
            {
                Toast.makeText(this, "Picture type is incorrect", Toast.LENGTH_LONG).show();
            }   
        }
        else
        {
            Toast.makeText(this, "Please re-select the picture", Toast.LENGTH_LONG).show();
        }   
    }
    else
    {
        Toast.makeText(this, "Please re-select the picture", Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}   

Thank you so so much for your help!

Was it helpful?

Solution

This is my code to show you.

public void initView() {
    AlertDialog.Builder builder =  new AlertDialog.Builder(repair.this).setMessage("Select")
        .setPositiveButton("Camera", new DialogInterface.OnClickListener() {        
        public void onClick(DialogInterface dialog, int i) {        
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            photoUri = getOutputMediaFileUri(3);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);             
            startActivityForResult(intent, 1);
        }       
    }).setNegativeButton("Album",  new DialogInterface.OnClickListener() {          
        public void onClick(DialogInterface dialog, int i) {                
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, 2);               
        }
   });
    AlertDialog dialog = builder.show();
    TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    dialog.show();
}

public Uri getOutputMediaFileUri(int type){
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type) {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Camera"); // Storage Folder

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("Camera", "Oops! Failed create "+ "Camera" + " directory"); // Error warning
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == 3) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); //Storage Name
    } else {
        return null;
    }
    return mediaFile;
}

Hope it may help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top