上传图像应用程序在模拟器上运行正常,但无法在测试手机上上传图像

时间:2014-03-13 10:58:40

标签: android

上传图像应用程序:它在模拟器上工作正常,但无法在测试手机上传图像。它从图库中选择图片,但在Imageview中不显示任何内容。

 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));           
        }
      }
    }

2 个答案:

答案 0 :(得分:0)

如果您从移动设备获取内容,则以下代码必须在清单文件中写入:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

使用以下代码,您将获得解决方案..

   `buttonLoadImage.setOnClickListener(new Button.OnClickListener(){ 
  @Override
   public void onClick(View v) 
   {
    // 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);

      if (resultCode == RESULT_OK)
        {
         Uri targetUri = data.getData();
         Bitmap bitmap;
         try 
         {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));

            targetImage.setImageBitmap(bitmap);
          } 
        catch (FileNotFoundException e) 
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }`

答案 1 :(得分:0)

使用以下方法解码文件路径。

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);
相关问题