android - 无法使用 google photos 上传图片

标签 android crop google-photos

我使用这段代码从图库中获取图像,然后在保存之前裁剪图像。它运行良好,适用于内置图库的 android,但在 onActivityResult 方法中给出 NullPointerException,我们在 android 上使用 google photos 应用程序时获得 extras.getParcelable("data")。任何帮助,将不胜感激。提前致谢:D

//This is called in oncreate() on clicking the upload from gallery button.
Intent galleryIntent = new Intent(Intent.ACTION_PICK ,    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
galleryIntent.putExtra("crop", "true");
startActivityForResult(galleryIntent,PICK_FROM_FILE);

//This is called on onActivityResult() method
if (requestCode == PICK_FROM_FILE && data != null) {
    Bundle extras = data.getExtras();
    //get the cropped bitmap from extras
    Bitmap thePic = extras.getParcelable("data");
    //do whatever with thePic
}

最佳答案

它对我有用。

//This is my onActivityResult method.
if (resultCode == RESULT_OK && data != null) {
    final Uri selectedImage = data.getData();

    String root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    File createDir = new File(root + "AppName" + File.separator);
    if (!createDir.exists()) {
        createDir.mkdirs();
    }
    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyhhmmss");
    String format = s.format(new Date());
    File file = new File(root + "AppName" + File.separator + format);

    if (!file.exists()) {
        try {
            file.createNewFile();
            copyFile(new File(getRealPathFromURI(selectedImage)), file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    String filePath = file.GetAbsolutePath();
    Bitmap bitmap = BitmapFactory.decodeFile(filepath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos);
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();
    Bitmap bmp = Bitmap.createScaledBitmap(bitmap, 100, 100, true);

    mImageView.setImageBitmap(bmp);

}

这是我在此使用的 copyFile 方法。

private void copyFile(File sourceFile, File destFile) throws IOException {
    if (!sourceFile.exists()) {
        return;
    }

    FileChannel source = null;
    FileChannel destination = null;
    source = new FileInputStream(sourceFile).getChannel();
    destination = new FileOutputStream(destFile).getChannel();
    if (destination != null && source != null) {
        destination.transferFrom(source, 0, source.size());
    }
    if (source != null) {
        source.close();
    }
    if (destination != null) {
        destination.close();
    }
}

希望它也对你有用:D

关于android - 无法使用 google photos 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276256/

相关文章:

android - 如何让一个屏幕包含google MapView和ListView?

python - 检测图像中的文本位置并在 Python 中裁剪它

android - 从 Google 照片提供商选择视频不会提供 mimetype 信息

java - 如何使用新的 Google Photos API 获取用户相册列表

java - Android - 以编程方式执行拖放

android - 找不到类 : Empty test suite when running unit tests in Android Studio

iphone - 从裁剪图像中删除不需要的区域

photo - 如何从 google photos api 获取图像总数

java - Android 应用程序仅在我从 Android Studio 安装时有效但当我直接在其他手机上安装 APK 时无效

java - 如何在三星 Galaxy S4 的相机中拍摄图像后裁剪图像