为什么Load image在模拟器上工作但在s4上不工作?

时间:2015-02-17 15:09:38

标签: java android xml imageview

我正在尝试在imageview中将图片加载到我的应用中。然而,每当我在我的S4(运行4.4.2)上尝试它时,我点击我要加载的图像。它说“不幸的是,E-textHome已经停止了。”但是,当我在我的模拟器上尝试应用程序时,这是一个Nexus7(运行4.1.3)它完全正常。所以我想知道我做错了什么,我如何让它在我的s4上工作?谢谢!

代码: -

public class EncryptImg extends ActionBarActivity implements OnClickListener{

    private static int LOAD_IMAGE_RESULTS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.encryptimg);

        Button galleryBrowse =  (Button) findViewById(R.id.browseGallerybtn1);
        galleryBrowse.setOnClickListener(this); /*Set OnClickListener for to listen for the galerryBrowse button*/

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId() == R.id.browseGallerybtn1){
            Intent loadImgIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
            startActivityForResult(loadImgIntent, LOAD_IMAGE_RESULTS);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ImageView imgEncrypt = (ImageView) findViewById(R.id.encryptImgView);

        // http://www.itcuties.com/android/pick-image-from-gallery/.

        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            // Now we need to set the GUI ImageView data with data read from the picked file.
            imgEncrypt.setImageBitmap(BitmapFactory.decodeFile(imagePath));

            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }

}

XML文件: -

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/browseGallerybtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="20dp"
        android:text="Browse Gallery" />

    <ImageView
        android:id="@+id/encryptImgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如果您说应用程序正在使用模拟器,那么它必须对清单文件中的权限执行某些操作。确保您已经提供了所有必需的权限,例如'READ_EXTERNAL_STORAGE'等。