Android 上传图像应用程序在模拟器上工作正常,但无法在测试手机上上传图像
上传图像应用程序:它在模拟器上工作正常,但无法在测试手机上上传图像。它从图库中选择图片,但不在Imageview中显示任何内容。Android 上传图像应用程序在模拟器上工作正常,但无法在测试手机上上传图像,android,Android,上传图像应用程序:它在模拟器上工作正常,但无法在测试手机上上传图像。它从图库中选择图片,但不在Imageview中显示任何内容。 public class Uploadimage extends Activity { public static int requestCode=1; Button b1; @Override public void onCreate(Bundle savedInstanceState) {
public class Uploadimage extends Activity {
public static int requestCode=1;
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uploadimage);
b1= (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, requestCode);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data) ;
if (requestCode==1 && resultCode==RESULT_OK) {
Uri image = data.getData();
String [] filepath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(image, filepath, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(filepath[0]);
String path= cursor.getString(column);
cursor.close();
ImageView imageview= (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
}
如果您从手机获取内容,则必须在清单文件中写入以下代码:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
使用以下方法解码文件路径
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 4;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2); // this bmp object of Bitmap is global and you can set it to your ImageView.
}
你有什么错误吗?您是否添加了清单文件的权限?您需要在图像集之前将图像转换为位图。如果您只关心将图像设置为imageView,请按Uri设置imageView,如此imageView.setImageUri(图像);不我无法得到任何错误。您是否添加了写入和读取外部存储的权限?已经这样做了。。这就是它在emulator上运行良好的原因。我不知道为什么无法加载图片phone@dipali那你呢???@PiyushGupta heiyyee它起作用了。。。谢谢但我可以知道为什么我以前的代码在emulator上工作,而不是在设备上吗?在设备中,映像可能以MB为单位。在某种程度上,我们可以说,也有从相机捕获的图像,所以它将以MB为单位。所以基本上它不能被简单地解码。为此,我们必须使用这个。
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 4;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2); // this bmp object of Bitmap is global and you can set it to your ImageView.
}
String path= cursor.getString(column);
decodeFile(path);