简体   繁体   English

Android图像选择器ImageView无法正常工作

[英]Android image picker ImageView not working

MY android app hangs the moment I select the picture ,there are no exception and I can't understand why,please help the code is below 我的android应用程序在我选择图片的那一刻挂起,没有异常,我也不明白为什么,请帮助下面的代码

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class SelectPhoto extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photopick_layout);
        Button btn=(Button)findViewById(R.id.clickme);

        btn.setOnClickListener(this);


    }

     public void onClick(View arg0) {
           // TODO Auto-generated method stub
           Intent intent = new Intent(Intent.ACTION_PICK,
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
           startActivityForResult(intent, 0);
          }

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

      super.onActivityResult(requestCode, resultCode, data);
      TextView textTargetUri = (TextView)findViewById(R.id.targeturi);
      if (resultCode == RESULT_OK){
       Uri targetUri = data.getData();
       textTargetUri.setText(targetUri.toString());
       Intent mi = new Intent(this , ImgDisplay.class);
       mi.putExtra(getString(R.string.app_name),targetUri);
       startActivity(mi);

      }

     }
     }

Imagedisplay Activity this is the part where it should display the selected image form its uri Imagedisplay Activity这是应该从其uri显示所选图像的部分

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class ImgDisplay extends Activity {
    private String a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_display);

 a =getIntent().getStringExtra(getString(R.string.app_name));




 Uri myUri = Uri.parse(a);

 ImageView b=(ImageView) findViewById(R.id.image_view);
 Bitmap myBitmap = BitmapFactory.decodeFile(a);
 b.setImageBitmap(BitmapFactory.decodeFile(a));
 b.setImageBitmap(myBitmap);



    }

}

photo pick layout this is the first layout where it asks you to select the image from the gallary 照片选择布局,这是第一个要求您从图库中选择图像的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:background="#00AEC5">

    <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/btn2"
>

    <TextView
  android:id="@+id/targeturi"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />

 <Button android:id="@+id/clickme" 
 android:text="@string/pick" 
 style="@style/style1"
android:layout_centerInParent="true"
 ></Button>


  </RelativeLayout>
</LinearLayout>

**Image_display layout* this is the layout to display the image ** Image_display layout *这是显示图像的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:background="#00AEC5">

    <ImageView android:id="@+id/image_view"     
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:adjustViewBounds="true"  
  android:maxWidth="42dp"  
  android:maxHeight="42dp"  
  android:scaleType="fitCenter"  
  android:layout_marginLeft="3dp"  

  ></ImageView>
</LinearLayout>

I'm sure that this is pretty easy but I'm new to this 我敢肯定这很容易,但是我是新手

您应该在onActivityResult中检查requestCode( 在此处为示例),如果结果属于您的请求,则处理该结果,仅在不属于该请求时才调用super。

use 采用

Intent.ACTION_GET_CONTENT 意图。ACTION_GET_CONTENT

instead. 代替。 Here is my image picker code that works perfectly . 这是我的图像选择器代码,可以很好地工作。

public class GalleryActivity extends Activity {

static Bitmap bmp;
ImageView GalImg;
Uri imageUri;
protected static final int PHOTO_PICKED = 0;
// values for scaling image
    protected int outputX = 320;
    protected int outputY = 480;
    protected int aspectX = 2;
    protected boolean scale = true;
    protected int aspectY = 3;
    Bundle extra;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("gal", "oncreate");
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.gallery_screen);
     extra = getIntent().getExtras();
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.putExtra("crop", "true");
    photoPickerIntent.putExtra("aspectX", aspectX);
    photoPickerIntent.putExtra("aspectY", aspectY);
    photoPickerIntent.putExtra("outputX", outputX);
    photoPickerIntent.putExtra("outputY", outputY);
    photoPickerIntent.putExtra("scale", scale);
    File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");      
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, PHOTO_PICKED);

}

@Override
protected void onActivityResult(int requestCode, int resultcode, 
        Intent intent) {
    super.onActivityResult(requestCode, resultcode, intent);
    Log.i("gal", "onActivity");
    if (requestCode == 0) {

        if (intent != null && resultcode == RESULT_OK) {
            Log.i("gal", "if request ok");
            //Uri selectedImage = intent.getData();// fetching uri of selected image                                                        // image


            //Bitmap bMap = (Bitmap) intent.getExtras().get("data");

            /*String[] filePathColumn = { MediaStore.Images.Media.DATA };// string data of image                                                                            // containing

            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            // The cursor query returns with the path, but you don't know
            // which column it's in until you use the columnIndex code.
            // That simply gets the number of the column based on its name,
            // the same one used in the filtering process.
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);// file path of image

            cursor.close();//Closes the Cursor, releasing all of its resources 


            bmp = BitmapFactory.decodeFile(filePath);*/

            //saving this bitmap into static photo which will have camera pic if camera is called otherwise gallery 
            //is selected and gallery image gets saved in it

            //PlayMenuActivity.photo=bmp;========
            //PlayMenuActivity.photo=bMap;


             Uri selectedImage = imageUri;
             getContentResolver().notifyChange(selectedImage, null);                
             ContentResolver cr = getContentResolver();
             Bitmap bitmap;
             try {
                 bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);  
                 PlayMenuActivity.photo=bitmap;
                 Log.i("gal",String.valueOf(bitmap));
                 Log.i("photo", "data.getAction() is not null. setting image.");

             } catch (Exception e) {
                 //Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                 Log.i("photo", e.toString());
             }

            Intent i=new Intent(GalleryActivity.this,ShowActivity.class);
            i.putExtra("category","gallery");
            startActivity(i);



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

    }
}

Hope this helps . 希望这可以帮助 。

好的,谢谢大家,我知道它是可耻的,但是唯一的问题是我忘了在清单文件中包括“ ImgDisplay”活动:)否则代码可以正常使用:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM