尝试上传图片以解析

时间:2016-01-26 19:07:55

标签: android parse-platform

我尝试使用以下代码上传图片文件:

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并且我试图找出exception是什么,但它在logcat甚至我的Toast中没有显示任何内容/ p>

我在这方面遇到异常:

} 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

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

任何想法可能是什么异常以及为什么我无法在我的LogCat或Toast上看到它?或者我的方法做错了什么? mMdeiaUri是图片的Uri

谢谢!任何正确的指导对我都有帮助

在日志调用上得到类似这样的内容:

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

1 个答案:

答案 0 :(得分:1)

Uri抓取部分可能有问题。您可以尝试此过程从存储设备获取图像并将其上载到解析云。

假设您有一个按钮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
         **********************/

    }

}

希望这适合你。