Android的 - 从图库中选择图片,并将其存储在一个文件类型变量变量、文件类型、图库、图片

2023-09-06 22:53:34 作者:该用户是小可爱

我是一个新的Andr​​oid开发。我想选择一个图像或Android设备的多媒体资料中的视频。它存储在键入文件的变量。我这样做,因为我需要上传的图片/视频的保管箱使用从我的应用程序在Android的API。构造函数的类型的文件的第四个参数。我不知道,是什么传递的文件,因为所有的我找遍显示屏按使用的网址并作出选择位图中的ImageView图像的例子。

  imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));
 

下面是code,我有。

 最终意图galleryIntent =新的意图(Intent.ACTION_GET_CONTENT);
//获取图像和视频,我用了一个* /
galleryIntent.setType(* / *);
startActivityForResult(galleryIntent,1);

保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == 1安培;&安培;结果code == RESULT_OK){
    乌里selectedImageUri = data.getData();
    的ImagePath = getPath(selectedImageUri);
    }
}

公共字符串getPath(URI URI){
    的String []投影= {MediaStore.Images.Media.DATA};
    光标光标= getContentResolver()查询(URI,投影,NULL,NULL,NULL);
    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    INT参数:columnIndex = cursor.getColumnIndex(投影[0]);
    字符串文件路径= cursor.getString(参数:columnIndex);
    cursor.close();
    yourSelectedImage = BitmapFactory.de codeFILE(文件路径);
    返回cursor.getString(Column_Index中);
}
 

解决方案

所有您需要做的仅仅是创建一个文件变量用图片您已的路径从图库中选择。改变你的 OnActivityResult 为:

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == 1安培;&安培;结果code == RESULT_OK){
    乌里selectedImageUri = data.getData();
    的ImagePath = getPath(selectedImageUri);
    文件镜像文件=新的文件(的ImagePath);
    }
}
 
在 Android 手机上恢复出厂设置后恢复照片的 4 种简单方法 新方法

I am a new to Android Development. I wish to select an image or a video from the Gallery of an Android Device. Store it in a variable of typeFile. I am doing this, since I need to upload the image/video on dropbox using the Android API from my application. The constructor takes in the fourth parameter of the type File. I am not sure, what to pass as a file since all the examples I searched display the image chosen in an ImageView by using the url and making a bitmap.

imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

Here is the code, I have.

final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
//to get image and videos, I used a */"
galleryIntent.setType("*/*"); 
startActivityForResult(galleryIntent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri);                
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();                
    int columnIndex = cursor.getColumnIndex(projection[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();        
    yourSelectedImage = BitmapFactory.decodeFile(filePath);
    return cursor.getString(column_index);
}

解决方案

All you need to do is just create a File variable with the path of image which you've selected from gallery. Change your OnActivityResult as :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri); 
    File imageFile = new File(imagepath);
    }
}

 
精彩推荐
图片推荐