0

这是我的裁剪代码,它在 2.2 和 2.3 上运行良好。但它不适用于 3.1。请解决这个问题。在 V2.3(HTC Wildfire S) 和 V3.1(Samsung 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);

}

}

我的布局看起来像这样,为此你需要在你的 sdcard “rawImage”上有一个图像。

 <?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>
4

1 回答 1

0

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

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

于 2012-04-20T14:03:14.873 回答