2015-06-22 27 views
0

我想在ImageView中設置圖像,我在第一個活動中檢索圖像路徑,並通過Intent作爲字符串將其傳遞給第二個活動。在第二個活動中,我將路徑設置爲ImageView。它工作正常,我需要將該圖片上傳到服務器。所以我將路徑解碼爲位圖。它會拋出一個OutOfMemoryError。如何解決這個問題?如何在Android中的位圖上解決OutOfMemoryError?

而當我使用前置攝像頭時,沒有任何問題。圖像上傳成功。問題在於設備前置攝像頭拍攝的圖像。這個問題的解決方案是什麼?誰能幫忙?

這裏是通過Intent到圖像路徑轉換爲字符串的代碼,並通過它:

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 


     Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, MediaStore.Images.Media.DATE_ADDED, null, "date_added ASC"); 
     if(cursor != null && cursor.moveToFirst()) 
     { 
      do { 
       Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))); 
       photoPath = uri.toString(); 

      }while(cursor.moveToNext()); 
      cursor.close(); 

      try { 
       Intent intent = new Intent(MainActivity.this, ImageUploadActivity.class); 
       intent.putExtra("ImagePath", photoPath); 
       MainActivity.this.startActivity(intent); 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(MainActivity.this, "Method invoked"+photoPath, Toast.LENGTH_SHORT).show(); 
      } 
     } 

在第二個活動接收意圖:

Intent camIntent = getIntent(); 
camPicPath = camIntent.getExtras().getString("ImagePath"); 
imageView = (ImageView) findViewById(R.id.imgView); 
imageView.setImageBitmap(BitmapFactory.decodeFile(camPicPath)); 
Toast.makeText(getApplicationContext(), "PATHe"+camPicPath, Toast.LENGTH_SHORT).show(); 
bitmap = (BitmapFactory.decodeFile(camPicPath)); 

方法上載文件:

class ImageUploadTask extends AsyncTask<Void, Void, String> { 
    @Override 
    protected String doInBackground(Void... unsued) { 
     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost("http://11.10.11.15/test/upload.php"); 

      MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
      byte[] data = bos.toByteArray(); 


      /* entity.addPart("uploaded_file", new ByteArrayBody(data, 
        "myImage.jpg"));*/ 

      // String newFilename= filename.concat("file"); 
      // newFilename=filename+newFilename; 

      entity.addPart("uploaded_file", new ByteArrayBody(data, 
        filename)); 
      // Log.e(TAG, "Method invoked"); 
      httpPost.setEntity(entity); 
      HttpResponse response = httpClient.execute(httpPost, 
        localContext); 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          response.getEntity().getContent(), "UTF-8")); 

      StringBuilder builder = new StringBuilder(); 
      String aux = ""; 

      while ((aux = reader.readLine()) != null) { 
       builder.append(aux); 
      } 

      String sResponse = builder.toString(); 


      return sResponse; 
     } catch (Exception e) { 
      if (dialog.isShowing()) 
       dialog.dismiss(); 
      Toast.makeText(getApplicationContext(), "Exception Message 1", Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
      return null; 
     } 
+0

你必須壓縮圖像。 –

+0

訪問[Android管理位圖內存](https://developer.android.com/training/displaying-bitmaps/manage-memory.html) – Bharatesh

回答

0

使用以下方法:

Bitmap bm = ShrinkBitmap(imagefile, 300, 300); 
    image.setImageBitmap(bm); 

Bitmap ShrinkBitmap(String file, int width, int height) { 
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
    bmpFactoryOptions.inJustDecodeBounds = true; 
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float) height); 
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float) width); 
    if (heightRatio > 1 || widthRatio > 1) { 
      if (heightRatio > widthRatio) { 
       bmpFactoryOptions.inSampleSize = heightRatio; 
      } else { 
       bmpFactoryOptions.inSampleSize = widthRatio; 
      } 
    } 
    bmpFactoryOptions.inJustDecodeBounds = false; 
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
    return bitmap; 
} 

或設置圖像的位圖時,像這樣使用inSampleSize

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 4; 
imageView.setImageBitmap(BitmapFactory.decodeFile(path, options)); 
+0

非常感謝。這種方法解決了我的問題。 – Gibs

0

您可以在下面屬性添加到高存儲器manifiest文件的應用程序標記。

android:largeHeap="true" 
相關問題