选择图像意图独立于设备

时间:2014-07-15 15:32:35

标签: android android-intent device

我正试图让Intent允许用户从手机中挑选照片或视频,然后将图片发布到服务器上。

我是这样做的:

public void lookForMedia(View v) {
    KeyboardHelper.hideKeyboard(this, postContentEtx);
    Intent myIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    myIntent.setType("image/*,video/*");
    myIntent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(myIntent, MEDIA_GALLERY);
}

然后:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case MEDIA_GALLERY:
            if (resultCode == RESULT_OK) {
                if (requestCode == PICTURE_SELECTED) {
                    mediaUri = data.getData();
                    ContentResolver cR = NewPostActivity.this.getContentResolver();
                    if (mediaUri == null) {
                        Toast.makeText(NewPostActivity.this, "Error while retrieving the media", Toast.LENGTH_LONG).show();
                        return;
                    }

                    String type = cR.getType(mediaUri);
                    if (type.startsWith("image")) {
                        mediaThumbnailImv.setImageURI(mediaUri);
                        videoUri = null;
                        thumbnailUri = null;
                        mediaRll.setVisibility(View.VISIBLE);
                        mediaThumbnailImv.setVisibility(View.VISIBLE);
                        mediaThumbnailNetworkImv.setVisibility(View.GONE);
                    } else if (type.startsWith("video")) {
                        videoUri = mediaUri;
                        mediaUri = null;
                        Bitmap videoThumbnail = ThumbnailUtils.createVideoThumbnail(FileUtil.getRealPathFromURI(NewPostActivity.this, videoUri), MediaStore.Images.Thumbnails.MINI_KIND);
                        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                        FileOutputStream out = null;
                        try {
                            String filename = "thumb_" + timeStamp + "kfkb.png";
                            out = new FileOutputStream(NewPostActivity.this.getFilesDir() + filename);
                            videoThumbnail.compress(Bitmap.CompressFormat.PNG, 90, out);
                            thumbnailUri = Uri.fromFile(new File(NewPostActivity.this.getFilesDir() + filename));
                            mediaThumbnailImv.setImageURI(thumbnailUri);

                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                out.close();
                            } catch (Throwable ignore) {
                            }
                        }

                        mediaRll.setVisibility(View.VISIBLE);
                        mediaThumbnailImv.setVisibility(View.VISIBLE);
                        mediaThumbnailNetworkImv.setVisibility(View.GONE);
                    } else {
                        Toast.makeText(NewPostActivity.this, "Invalid media", Toast.LENGTH_LONG).show();
                        return;
                    }

                }

            }
            break;
    }
}

它在我试过的设备(Nexus 5,LG G3,三星Galaxy S3,Wiko Cink Five)上工作正常,但是有些用户告诉我他们在点击按钮时看不到他们的照片或视频(似乎它出现在旧版本的Android上,但我现在无法获得更准确的信息)。有没有不同的方法来做到这一点,并确保它适用于大多数设备?有什么我做错了吗?

1 个答案:

答案 0 :(得分:0)

首先,我认为您并不理解跨平台是正确的。

Cross-Platform - 独立于OS代码,将在每个操作系统上同等执行。对于每个操作系统 - 将编译可执行文件。对于Android - apk 。对于Windows - exe 。等...

解决你的问题。如果用户抱怨逻辑工作不正确,我建议你做几件事:

  • 使用 device_info 将堆栈跟踪记录到转储文件到sdcard。在申请结束前询问用户 - 将此日志发送到您的电子邮箱。或类似的东西。

  • 尝试寻找选择图像的替代方法。对于最糟糕的情况 - 编写您自己的活动和扫描系统的图像。