无法在Android 2.2上打开图库

时间:2012-08-05 18:00:33

标签: android android-intent gallery image

我做了两个按钮。一个打开相机,一个打开画廊。相机一个工作正常,但画廊一个没有...它打开,但当我从画廊选择一个图片时,应用程序意外关闭。

package com.example.me;

import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener{


Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
private static final int SELECT_IMAGE = 1;
Button clickk;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
    bmp = BitmapFactory.decodeStream(is);
}


private void initialize() {
    // TODO Auto-generated method stub
    iv = (ImageView) findViewById (R.id.imageView1);
    b = (Button) findViewById (R.id.btakePic);
    clickk = (Button) findViewById (R.id.bChose);
    b.setOnClickListener(this);
    clickk.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){

    case R.id.btakePic:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, cameraData);
        break;
    case R.id.bChose:
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);//
        startActivityForResult(Intent.createChooser(intent, "Select  Picture"),SELECT_IMAGE);
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    }

}
}   

以下是日志:

08-05 11:30:19.455: E/ActivityThread(5576): Activity           com.android.internal.app.ChooserActivity has leaked IntentReceiver   com.android.internal.app.ResolverActivity$1@412fd3e0 that was originally registered here.  Are you missing a call to unregisterReceiver()?
08-05 11:30:19.455: E/ActivityThread(5576): android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@412fd3e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1099)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1086)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1080)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
08-05 11:30:19.455: E/ActivityThread(5576):     at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:65)
08-05 11:30:19.455: E/ActivityThread(5576):     at    com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:101)
08-05 11:30:19.455: E/ActivityThread(5576):     at    com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.Activity.performCreate(Activity.java:4465)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.os.Looper.loop(Looper.java:137)
08-05 11:30:19.455: E/ActivityThread(5576):     at android.app.ActivityThread.main(ActivityThread.java:4514)
08-05 11:30:19.455: E/ActivityThread(5576):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:30:19.455: E/ActivityThread(5576):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:30:19.455: E/ActivityThread(5576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-05 11:30:19.455: E/ActivityThread(5576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-05 11:30:19.455: E/ActivityThread(5576):     at dalvik.system.NativeStart.main(Native Method)
08-05 11:30:19.471: D/OpenGLRenderer(5576): Flushing caches (mode 1)
08-05 11:30:42.932: D/AndroidRuntime(5576): Shutting down VM
08-05 11:30:42.932: W/dalvikvm(5576): threadid=1: thread exiting with uncaught exception   (group=0x40be41f8)
08-05 11:30:42.932: E/AndroidRuntime(5576): FATAL EXCEPTION: main
08-05 11:30:42.932: E/AndroidRuntime(5576): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/774 }} to activity {com.example.me/com.example.me.MainActivity}: java.lang.NullPointerException
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
08-05 11:30:42.932: E/AndroidRuntime(5576):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
08-05 11:30:42.932: E/AndroidRuntime(5576):     at android.app.ActivityThread.access$1100(ActivityThread.java:128)
08-05 11:30:42.932: E/AndroidRuntime(5576):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
08-05 11:30:42.932: E/AndroidRuntime(5576):     at android.os.Handler.dispatchMessage(Handler.java:99)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at android.os.Looper.loop(Looper.java:137)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at android.app.ActivityThread.main(ActivityThread.java:4514)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at java.lang.reflect.Method.invokeNative(Native Method)
  08-05 11:30:42.932: E/AndroidRuntime(5576):   at java.lang.reflect.Method.invoke(Method.java:511)
  08-05 11:30:42.932: E/AndroidRuntime(5576):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at dalvik.system.NativeStart.main(Native Method)
  08-05 11:30:42.932: E/AndroidRuntime(5576): Caused by: java.lang.NullPointerException
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at com.example.me.MainActivity.onActivityResult(MainActivity.java:72)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)
 08-05 11:30:42.932: E/AndroidRuntime(5576):    ... 11 more

这是我现在拥有的。它仍然无法正常工作。我猜我应该把相机结果和画廊结果放在一起?

     @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     // TODO Auto-generated method stub
     super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == RESULT_OK){
         Bundle extras = data.getExtras();
         bmp = (Bitmap) extras.get("data");
         iv.setImageBitmap(bmp);
         if (requestCode == SELECT_IMAGE) {
            if(resultCode == RESULT_OK){  
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,   filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();
                Log.i(myTag, filePath); 
                File imgFile = new File(filePath);
                //Get your image out of the file
                //do with it what you will.
                Bundle extras1 = data.getExtras();
                bmp = (Bitmap) extras1.get("data");
                iv.setImageBitmap(bmp);

我的标签也应该是一个字符串吗?

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == cameraData)
    if (resultCode == RESULT_OK)   {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);



        if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView)   findViewById(R.id.imageView1);
               imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

3 个答案:

答案 0 :(得分:0)

您需要以不同于onActivityResult()中的相机来处理图库。这是一个粗略的例子,说明了从图库中处理选择图像需要做些什么。

if (requestCode == SELECT_IMAGE) {
    if(resultCode == RESULT_OK){  
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        Log.i(myTag, filePath); 
        File imgFile = new File(filePath);
        //Get your image out of the file
        //do with it what you will.
    }
}

答案 1 :(得分:0)

试试这个代码......

 yourGalleryButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
                                 photoPickerIntent.setType("image/*");
                                startActivityForResult(photoPickerIntent, 1);
            }
        });

     protected void onActivityResult(int requestCode, int resultcode, Intent intent)
      {
          super.onActivityResult(requestCode, resultcode, intent);


          if (requestCode == 1)
          {
              if (intent != null && resultcode == RESULT_OK)
              {             

                    Uri selectedImage = intent.getData();

                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    cursor.close();

                    if(bmp != null && !bmp.isRecycled())
                    {
                        bmp = null;               
                    }

                    bmp = BitmapFactory.decodeFile(filePath);


              }
              else
              {
                  Log.d("Status:", "Photopicker canceled");           
              }
          }
      }
    }

这是简单易用的代码.....

答案 2 :(得分:0)

这是有效的代码。我将-------放在我移动的支架上以使活动起作用。下面有3个刹车,我刚刚搬了一个。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);



    if (requestCode == cameraData)
    if (resultCode == RESULT_OK)   {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    -----}


        if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));


            }



    }