android获取本地图片路径为https,如何获取本地phone路径?
android getting local image path as https, how to get local phone path?
我从图库中获取了用户选择的图像并想在图像视图中显示它
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE: ", selectedImagePath);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}
有些图片的路径为:/storage/sdcard0/WhatsApp/Media/WhatsApp Images/IMG-20150407-WA0013.jpg 可以正常显示
还有一些图片给出了 google 下载 link 的路径。示例:https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg(出于隐私原因在 post 中更改了路径)
这些图片没有显示。
我有一个这样的例子,
我正在获取 Uri 作为内容://com.sec.android.gallery3d.provider/picasa/item/5913341278276321746
并将 selectedImagePath 设为 https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg
如何获取此类图像的 selectedImagePath 作为 phone 中的路径而不是 Web 位置?
试试这个来下载位图:
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
正如我在评论中所述,您应该使用 Picasso 或 Glide 等库,因为在它们的实现中有很多角落案例已解决,例如缓存系统、网络故障处理、图像效果和转换、内存性能等.
如果你想以更简单的方式做到这一点并且"by hand"你应该考虑这个:
private class NetworkImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Void doInBackground(String... params) {
try {
InputStream in = new URL(params[0]).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null)
mImageView.setImageBitmap(result);
}
}
}
只需调用:new NetworkImageAsyncTask("url").execute();
你应该使用 AsyncTask
因为这个 "network methods" 不应该在主线程中完成。
注意:此解决方案不包括内存泄漏安全代码,它只是一个示例方法
编辑(评论回复):
试试这个:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/jpg");
...
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return path;
我使用以下方法解决了它:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,
"Share Photo"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
selectedImageUri = selectedImageUri .replace("com.android.gallery3d","com.google.android.gallery3d");
if (selectedImageUri .startsWith("content://com.google.android.gallery3d")
|| selectedImageUri .startsWith("content://com.sec.android.gallery3d.provider") ) {
selectedImagePath = PicasaImage(selectedImageUri);
}
else
selectedImagePath = getPath(selectedImageUri);
}
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE PATH: ", selectedImagePath);
}
}
private String PicasaImage(Uri imageUri) {
File cacheDir;
// if the device has an SD card
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),".OCFL311");
} else {
// it does not have an SD card
cacheDir = getCacheDir();
}
if(!cacheDir.exists()) cacheDir.mkdirs();
File f = new File(cacheDir, "tempPicasa");
try {
InputStream is = null;
if (imageUri.toString().startsWith("content://com.google.android.gallery3d")
|| imageUri.toString().startsWith("content://com.sec.android.gallery3d.provider")) {
is = getContentResolver().openInputStream(imageUri);
} else {
is = new URL(imageUri.toString()).openStream();
}
OutputStream os = new FileOutputStream(f);
//Utils.InputToOutputStream(is, os);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
return f.getAbsolutePath();
} catch (Exception ex) {
Log.i(this.getClass().getName(), "Exception: " + ex.getMessage());
// something went wrong
ex.printStackTrace();
return null;
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}
我从图库中获取了用户选择的图像并想在图像视图中显示它
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE: ", selectedImagePath);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}
有些图片的路径为:/storage/sdcard0/WhatsApp/Media/WhatsApp Images/IMG-20150407-WA0013.jpg 可以正常显示
还有一些图片给出了 google 下载 link 的路径。示例:https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg(出于隐私原因在 post 中更改了路径)
这些图片没有显示。
我有一个这样的例子,
我正在获取 Uri 作为内容://com.sec.android.gallery3d.provider/picasa/item/5913341278276321746
并将 selectedImagePath 设为 https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg
如何获取此类图像的 selectedImagePath 作为 phone 中的路径而不是 Web 位置?
试试这个来下载位图:
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
正如我在评论中所述,您应该使用 Picasso 或 Glide 等库,因为在它们的实现中有很多角落案例已解决,例如缓存系统、网络故障处理、图像效果和转换、内存性能等.
如果你想以更简单的方式做到这一点并且"by hand"你应该考虑这个:
private class NetworkImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Void doInBackground(String... params) {
try {
InputStream in = new URL(params[0]).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null)
mImageView.setImageBitmap(result);
}
}
}
只需调用:new NetworkImageAsyncTask("url").execute();
你应该使用 AsyncTask
因为这个 "network methods" 不应该在主线程中完成。
注意:此解决方案不包括内存泄漏安全代码,它只是一个示例方法
编辑(评论回复):
试试这个:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/jpg");
...
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return path;
我使用以下方法解决了它:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,
"Share Photo"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
selectedImageUri = selectedImageUri .replace("com.android.gallery3d","com.google.android.gallery3d");
if (selectedImageUri .startsWith("content://com.google.android.gallery3d")
|| selectedImageUri .startsWith("content://com.sec.android.gallery3d.provider") ) {
selectedImagePath = PicasaImage(selectedImageUri);
}
else
selectedImagePath = getPath(selectedImageUri);
}
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE PATH: ", selectedImagePath);
}
}
private String PicasaImage(Uri imageUri) {
File cacheDir;
// if the device has an SD card
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),".OCFL311");
} else {
// it does not have an SD card
cacheDir = getCacheDir();
}
if(!cacheDir.exists()) cacheDir.mkdirs();
File f = new File(cacheDir, "tempPicasa");
try {
InputStream is = null;
if (imageUri.toString().startsWith("content://com.google.android.gallery3d")
|| imageUri.toString().startsWith("content://com.sec.android.gallery3d.provider")) {
is = getContentResolver().openInputStream(imageUri);
} else {
is = new URL(imageUri.toString()).openStream();
}
OutputStream os = new FileOutputStream(f);
//Utils.InputToOutputStream(is, os);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
return f.getAbsolutePath();
} catch (Exception ex) {
Log.i(this.getClass().getName(), "Exception: " + ex.getMessage());
// something went wrong
ex.printStackTrace();
return null;
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}