Android Studio从Gallery Intent获取文件 [英] Android Studio get File from Gallery Intent

查看:164
本文介绍了Android Studio从Gallery Intent获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的回答是可以从库请求中获取原始文件,如果可以的话我该怎么办?该代码对我不起作用.

Short answer is it possible to get original file from Gallery request,and if it possible how can i do it? This code doesn't work for me.

Uri uri = data.getData();
File file = new File(uri.getPath());

和长答案)): 我使用此代码使图库具有意图

And the long Answer)): I use this code to make gallery intent

addGallery.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
             Intent intent = new Intent(Intent.ACTION_PICK);
             intent.setType("image/*");
             startActivityForResult(intent, GALLERY_IMAGE_REQUEST);
        }
    });

在mu onActivityResult中,我使用此代码

In mu onActivityResult i use this code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK)
    {
        switch (requestCode)
        {
            case GALLERY_IMAGE_REQUEST:
                if (data != null)
                {
                    try
                    {
                        Uri uri = data.getData();
                        File file = new File(uri.getPath());
                        FileInputStream inputStream = new FileInputStream(file);
                        bitmap = BitmapFactory.decodeStream(inputStream);
                        imageView.setImageBitmap(bitmap);
                        inputStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
        }
    }
}

我无法获取文件.

使用相同的代码从数据中获取位图效果很好,但我需要从图库中获取确切的文件,而不仅仅是Uri或Bitmap.

The same code with getting bitmap from data works well but i need to get exactly file from gallery but not only Uri or Bitmap.

                 try
                    {
                        Uri uri = data.getData();
                        InputStream imageStream = getContentResolver().openInputStream(uri);
                        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageView.setImageBitmap(selectedImage);
                        imageStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }

推荐答案

如果要将图片从图库导入到您的应用程序中(如果您的应用程序是图片),则需要将其复制到应用程序数据文件夹中.

If you want to import a picture from gallery into your app (in a case your app own it), you need to copy it to your app data folder.

在您的onActivityResult()中:

in your onActivityResult():

    if (requestCode == REQUEST_TAKE_PHOTO_FROM_GALLERY && resultCode == RESULT_OK) {
        try {
            // Creating file
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.d(TAG, "Error occurred while creating the file");
            }

            InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
            FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
            // Copying
            copyStream(inputStream, fileOutputStream);
            fileOutputStream.close();
            inputStream.close();

        } catch (Exception e) {
            Log.d(TAG, "onActivityResult: " + e.toString());
        }
    }

创建文件方法:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

复制方法:

public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}

这篇关于Android Studio从Gallery Intent获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆