捕获HTC设备上的图像崩溃

时间:2012-07-02 05:51:35

标签: java android

您好我正在开发一个捕获图像和电子邮件的应用程序捕获图像在三星galaxy和索尼爱立信xperia工作正常,但它不能在HTC设备上工作任何人都知道原因??这是我拍摄图像的代码

try {
    String fileName = Image_name+".jpg";
    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
    //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
    outuri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //outuri = Uri.fromFile(photo);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
    cameraIntent.putExtra("return-data", true);
    startActivityForResult(cameraIntent, 2);
} catch (Exception e) {
    Toast.makeText(preview.this, ""+e, Toast.LENGTH_LONG).show();
}

这是我用来检索图像的代码

path = convertImageUriToFile(outuri, preview.this).getAbsolutePath();
arr.add(path);
try {
    bitmap = getImage(path);
    public static File convertImageUriToFile (Uri imageUri, Activity activity)  {
        Cursor cursor = null;
        try {
            String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
            cursor = activity.managedQuery(imageUri, proj, // Which columns to return
                    null,       // WHERE clause; which rows to return (all rows)
                    null,       // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
            int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
            if (cursor.moveToFirst()) {
                String orientation =  cursor.getString(orientation_ColumnIndex);
                return new File(cursor.getString(file_ColumnIndex));
            }
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public Bitmap getImage(String path) throws IOException
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        Bitmap targetBitmap=null;
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int[] newWH =  new int[2];
        newWH[0] = 1024;
        newWH[1] = (1024*srcHeight)/srcWidth;

        int inSampleSize = 1;
        while(srcWidth / 2 > newWH[0]){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }
//      float desiredScale = (float) newWH[0] / srcWidth;
        // Decode with inSampleSize
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
        ExifInterface exif = new ExifInterface(path);
        String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
        Matrix matrix = new Matrix();
        float rotation = rotationForImage(preview.this, Uri.fromFile(new File(path)));
        if (rotation != 0f) {
            matrix.preRotate(rotation);
        }
        int newh = ( w * sampledSrcBitmap.getHeight() ) /sampledSrcBitmap.getWidth();
        Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true);
        Bitmap resizedBitmap = Bitmap.createBitmap(
                r, 0, 0, w, newh, matrix, true);

        return resizedBitmap;
    }
}

1 个答案:

答案 0 :(得分:2)

嗯,Intent.putExtra(MediaStore.EXTRA_OUTPUT)中存在一个已知错误,它会导致应用程序崩溃。

查看我在问同一个问题时得到的答案:https://stackoverflow.com/a/10613299/1056359