为什么加载图像在模拟器上工作但在 s4 上不起作用?
Why does Load image work on emulator but not on s4?
我正在尝试在图像视图中将图像加载到我的应用程序中。然而,每当我在我的 S4 (运行 4.4.2) 上尝试它时,一旦我点击我想要加载的图像。它说 'Unfortunately, E-textHome has stopped.'。但是,当我在 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>
如果您说该应用程序正在模拟器上运行,那么它必须对清单文件中的权限执行某些操作。确保您已授予所有必需的权限,例如 'READ_EXTERNAL_STORAGE' 等
我正在尝试在图像视图中将图像加载到我的应用程序中。然而,每当我在我的 S4 (运行 4.4.2) 上尝试它时,一旦我点击我想要加载的图像。它说 'Unfortunately, E-textHome has stopped.'。但是,当我在 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>
如果您说该应用程序正在模拟器上运行,那么它必须对清单文件中的权限执行某些操作。确保您已授予所有必需的权限,例如 'READ_EXTERNAL_STORAGE' 等