我的相机功能无法正常工作

时间:2014-06-04 07:31:51

标签: android camera onactivityresult

这是我的相机代码。我正在编写一个使用startActivityForResult来处理结果的函数。但是当我选择ACTION_IMAGE_CAPTURE时,通常它会捕获图片,我可以去找这张照片使用的路径。但是,我在手机中找不到路径。看来这段代码无法在我的手机中使用,因此我无法在手机中保存图片。

我的手机型号是SONY xperia C和SONY xperia L.

以下是我的选择代码

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

这是我的onActivityResult代码。

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

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这是我向您展示的代码。

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

希望它可以帮到你。