myc_100 2013-04-16 07:42 采纳率: 10%
浏览 2744

不能从 Gallery 中加载图像

当点击 imageview 时,想让用户从 Gallery 中选择图像或使用 Camera 来拍照。 使用的下面的代码:

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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class UploadImageActivity extends Activity {
    private final int CAMERA_PICTURE = 1;
    private final int GALLERY_PICTURE = 2;
    private ImageView userPictureImageView;
    private Intent pictureActionIntent = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        userPictureImageView = (ImageView) findViewById(R.id.image_view);
        userPictureImageView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startDialog();
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_PICTURE) {
            Uri uri = data.getData();
            if (uri != null) {
                // User had pick an image.
                Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();
                // Link to the image
                final String imageFilePath = cursor.getString(0);
                File photos = new File(imageFilePath);
                Bitmap b = decodeFile(photos);
                b = Bitmap.createScaledBitmap(b, 150, 150, true);
                userPictureImageView.setImageBitmap(b);
                cursor.close();
            }
            else {
                Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
                toast.show();
            }
        }
        else if (requestCode == CAMERA_PICTURE) {
            if (data.getExtras() != null) {
                // here is the image from camera
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                userPictureImageView.setImageBitmap(bitmap);
            }
        }
    }

    private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
        catch (FileNotFoundException e) {
        }
        return null;
    }

    private void startDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
                pictureActionIntent.setType("image/*");
                pictureActionIntent.putExtra("return-data", true);
                startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
            }
        });

        myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(pictureActionIntent, CAMERA_PICTURE);
            }
        });
        myAlertDialog.show();
    }
}

照相机可以使用,但是Gallery 不能正常运行。由于某些原因总是崩溃。是什么原因造成的呢?
Error Log

04-14 01:09:30.566: E/AndroidRuntime(24630): FATAL EXCEPTION: main
04-14 01:09:30.566: E/AndroidRuntime(24630): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.csun.spotr/com.csun.spotr.UploadImageActivity}: java.lang.NullPointerException
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.os.Looper.loop(Looper.java:130)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.main(ActivityThread.java:3683)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at java.lang.reflect.Method.invoke(Method.java:507)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at dalvik.system.NativeStart.main(Native Method)
04-14 01:09:30.566: E/AndroidRuntime(24630): Caused by: java.lang.NullPointerException
04-14 01:09:30.566: E/AndroidRuntime(24630):    at com.csun.spotr.UploadImageActivity.onActivityResult(UploadImageActivity.java:42)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
04-14 01:09:30.566: E/AndroidRuntime(24630):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
04-14 01:09:30.566: E/AndroidRuntime(24630):    ... 11 more

我注意到如果我去掉 这一行,程序就不会崩溃。但是不能从Gallery点击图像。

  • 写回答

3条回答 默认 最新

  • balmy 2013-04-16 13:20
    关注

    可能Gallery那边没有传值吧,你要打log或调试看下 Uri uri = data.getData();这里的data到底是什么

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?