裁剪android 3.1的图像代码不起作用

裁剪android 3.1的图像代码不起作用,android,Android,这是我的裁剪代码,它在2.2和2.3上运行良好。但它不适用于3.1。请解决这个问题。在V2.3HTC Wildfire S和V3.1Samsung 10.1上测试,提前感谢 package com.androidworks; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import android.a

这是我的裁剪代码,它在2.2和2.3上运行良好。但它不适用于3.1。请解决这个问题。在V2.3HTC Wildfire S和V3.1Samsung 10.1上测试,提前感谢

package com.androidworks;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MediaStoreTest extends Activity {

protected static final int PHOTO_PICKED = 0;
private static final String TEMP_PHOTO_FILE = "tempPhoto.jpg";
private Button mBtn;
protected ImageView photo;
protected int outputX = 400;
protected int outputY = 600;
protected int aspectX = 1;
protected int aspectY = 1;
protected boolean return_data = false;
protected MediaStoreTest thiz;
protected boolean scale = true;
protected boolean faceDetection = true;
protected boolean circleCrop = false;
private final static String TAG = "MediaStoreTest";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    thiz = this;
    setContentView(R.layout.main);
    mBtn = (Button) findViewById(R.id.btnLaunch);
    photo = (ImageView) findViewById(R.id.imgPhoto);

    mBtn.setOnClickListener(new OnClickListener(){


        public void onClick(View v) {
            try {

                File picFile = new File(Environment.getExternalStorageDirectory(),"rawImge.png");
                Intent intent = new Intent("com.android.camera.action.CROP");
               intent.setDataAndType(Uri.fromFile(picFile),"image/*");

                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", aspectX);
                intent.putExtra("aspectY", aspectY);
                intent.putExtra("outputX", outputX);    
                intent.putExtra("outputY", outputY);
                intent.putExtra("scale", scale);
                intent.putExtra("return-data", return_data);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection",!faceDetection); // lol, negative boolean noFaceDetection
                if (circleCrop) {
                    intent.putExtra("circleCrop", true);
                }

                startActivityForResult(intent, PHOTO_PICKED);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(thiz, R.string.photoPickerNotFoundText, Toast.LENGTH_LONG).show();
            }
        }
        });

}

private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
}

private File getTempFile() {
    if (isSDCARDMounted()) {

        File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
        try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(thiz, R.string.fileIOIssue, Toast.LENGTH_LONG).show();
        }
        return f;
    } else {
        return null;
    }
}

private boolean isSDCARDMounted(){
    String status = Environment.getExternalStorageState();

    if (status.equals(Environment.MEDIA_MOUNTED))
        return true;
    return false;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case PHOTO_PICKED:
        if (resultCode == RESULT_OK) {
                if (data == null) {
                    Log.w(TAG, "Null data, but RESULT_OK, from image picker!");
                    Toast t = Toast.makeText(this, R.string.no_photo_picked,
                                             Toast.LENGTH_SHORT);
                    t.show();
                    return;
                }

                final Bundle extras = data.getExtras();
                if (extras != null) {
                        File tempFile = getTempFile();
                        // new logic to get the photo from a URI
                        if (data.getAction() != null) {
                            processPhotoUpdate(tempFile);
                        }                  
                }
        }
        break;
    }
}

/*
 *  processes a temp photo file from 
 */
private void processPhotoUpdate(File tempFile) {

    Log.w(TAG, "processPhotoUpdate is called");

    FileInputStream fisForResilt = null;
    Bitmap result = null;
    try {
        fisForResilt = new FileInputStream(tempFile);
         result = BitmapFactory.decodeStream(fisForResilt);
        fisForResilt.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(result.getWidth(),result.getHeight());
    params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    photo.setLayoutParams(params);
    photo.setImageBitmap(result);

}

}
我的布局是这样的,你需要在SD卡上有一个图像

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
 >
<TextView  
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/summary"
 />
 <Button
 android:id="@+id/btnLaunch"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Launch Media Gallery"
 />
 <ImageView
  android:id="@+id/imgPhoto"
 android:layout_width="400dip"
 android:layout_height="400dip"
 />
</LinearLayout>

裁剪意图不是公共API的一部分。只有一种解决方案可以保证对所有设备都有效:将裁剪代码集成到您自己的应用程序中。这很容易实现,因为官方android gallery应用程序是开源的:-


如果您不想这样做,您可以通过手动启动三星设备并观察logcat来尝试找出裁剪活动的名称。应该是com.android.sec.gallery3d.app.CropImage

请在您的问题上投入更多精力。“不工作”是没有意义的,请确切地告诉我结果是什么。@蒂姆:裁剪图像后,会出现一个保存图片的进度对话框,它会挂断。进度对话框根本不会运行。