2016-01-26 45 views
0

我想使用此代碼上傳圖片文件:未知異常,同時試圖上傳圖像解析

if (mMediaUri != null){ 
    Log.v("url = "+mMediaUri, ""); 

    //create parse object for image to upload 
    final ParseObject imageUpload = new ParseObject("TestNotes"); 
    try { 
     //convert image to bytes for upload. 
     byte[] fileBytes = FileHelper.getByteArrayFromFile(getContext(), mMediaUri); 
     if (fileBytes == null) { 
      //there was an error 
      Toast.makeText(getContext(), "There was an error. Try again!", Toast.LENGTH_LONG).show(); 
     } else { 
      fileBytes = FileHelper.reduceImageForUpload(fileBytes); 
      String fileName = FileHelper.getFileName(getContext(), mMediaUri, "image"); 
      final ParseFile file = new ParseFile(fileName, fileBytes); 
      imageUpload.saveEventually(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        if (e == null) { 
         imageUpload.put("File", file); 
         sendNote.saveInBackground(); 
         imageUpload.saveInBackground(new SaveCallback() { 
          @Override 
          public void done(ParseException e) { 
           Toast.makeText(getContext(), "Success Uploading iMage!", Toast.LENGTH_LONG).show(); 
          } 
         }); 
        } else { 
         //there was an error 
         //there was an error 
         Log.v(""+e.getMessage(), " Error Here"); 
         Toast.makeText(getContext(), "Error", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 
     } 
    } catch (Exception e1) { 
     Toast.makeText(getContext(), "Our Error", Toast.LENGTH_LONG).show(); 
     Log.v("our Error = "+ e1.getMessage(),""); 

     Toast.makeText(getContext(), e1.getMessage(), Toast.LENGTH_LONG).show(); 
    } 

,但它不工作我得到exception和我試過了TI弄清楚什麼exception是,但其未展示在此行中我logcat,甚至在我Toast

我得到任何的異常:

} catch (Exception e1) { 
    Toast.makeText(getContext(), "Our Error", Toast.LENGTH_LONG).show(); 
    Log.v("our Error = "+ e1.getMessage(),""); 
    Toast.makeText(getContext(), e1.getMessage(), Toast.LENGTH_LONG).show(); 
} 

我不知道爲什麼它顯示我的只有這吐司

Toast.makeText(getContext(), "Our Error" 

任何想法可能是例外,爲什麼我不能夠看到它在我的logcat或吐司?或者我的方法做錯了什麼?而mMdeiaUriUri的圖片,

謝謝!爲使任何指導的權利會因此有助於在日誌調用我

我得到這樣的:

01-27 00:51:43.799 2805-2805/pb.pocketboard I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 
01-27 00:51:43.799 2805-2805/pb.pd W/dalvikvm: VFY: unable to resolve virtual method 455: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
01-27 00:51:43.799 2805-2805/pb.pd D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
01-27 00:51:43.799 2805-2805/pb.pd I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 
01-27 00:51:43.799 2805-2805/pb.pd W/dalvikvm: VFY: unable to resolve virtual method 457: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
01-27 00:51:43.799 2805-2805/pb.pd D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
+2

因爲您將所有消息都分配給第一個參數,所以在詳細信道中不會看到任何內容。嘗試Log.v(「我們的錯誤」,e1.getMessage())。 – natario

+0

感謝您的回覆@mvai讓我試試這個 –

+0

@mvai請看看我更新的問題,我面臨這個Log.v問題一會兒,他們甚至沒有打印任何東西,即使按照您的建議嘗試 –

回答

1

有可能是壞了開放的獲取部分。您可以嘗試此過程以從您的存儲設備獲取映像並將其上傳到解析雲。

假設你有一個按鈕photoFromGallery挑選的圖片

photoFromGallery.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, 1); // Here, 1 is for image file 
     } 
    }); 

覆蓋onActivityResult得到與上傳過程中完成的。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 
    //requestCode = 1 for image file 
    if (requestCode == 1 && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); // Here you get the path of your image 
     cursor.close(); 

     Bitmap bmpget = BitmapFactory.decodeFile(filePath); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

     // Compress image to lower quality scale 1 - 100 (will be ignored for PNG type) 
     bmpget.compress(Bitmap.CompressFormat.JPEG, 90, stream); 
     byte[] image = stream.toByteArray(); 

     //Create a file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date()); 
     String filename = "img_"+timeStamp+".jpg"; 

     // Create the ParseFile 
     ParseFile file = new ParseFile(filename,image); 

     /********************** 
     * Code for saving your file to the parseCloud either on a specific row or new row 
     **********************/ 

    } 

} 

希望這適用於你。

+0

我已經改變了我的方法,因爲時間限制,但是非常感謝:) @Sudip –

+0

很高興聽到這個! :) @remyboys –