无法使用普通 setImageUri 和 Picasso 在 ImageView 上设置图像 - Android Nougat
Not able to set image on ImageView with normal setImageUri and with Picasso - Android Nougat
我正在尝试 select 图片库中的图像并将其发送给其他人 activity。它在 Kitkat 中运行良好,但在 android Nougat 中不起作用。我也尝试了普通的 setImageUri 和 Picasso。仍然无法在 Imageview 上渲染图像。请看下面的代码。
- MainActivity.java - 我有一个按钮,点击按钮我调用 intent 打开图库。
gallery_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/");
startActivityForResult(intent, SELECT_PHOTO);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PHOTO) {
Uri selectedImageUri = data.getData();
Log.d("vishal path", selectedImageUri.toString());
selectedImagePath = getPath(selectedImageUri);
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtra("selectedImg", selectedImagePath);
startActivity(intent);
}
}
}
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
// 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);
}
return uri.getPath();
}
通过上述方法,我能够从图库中获取图像路径。我可以在日志监视器上看到。
ImageActivity
public class ImageActivity 扩展 AppCompatActivity {
ImageView mainImg, brushImg, fontImg;
String imgPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
mainImg = (ImageView) findViewById(R.id.imageSetMain);
brushImg = (ImageView) findViewById(R.id.imageBrush);
fontImg = (ImageView) findViewById(R.id.imageFont);
mainImg.postInvalidate();;
Bundle extras = getIntent().getExtras();
imgPath = extras.getString("selectedImg");
Log.d("vishal received path", imgPath);
// mainImg.setImageURI(null);
// mainImg.setImageURI(Uri.parse(extras.getString("selectedImg")));
Picasso.with(ImageActivity.this).load(new File(imgPath)).into(mainImg);
brushImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ImageActivity.this, DrawActivity.class);
intent.putExtra("selectedImg", imgPath);
startActivity(intent);
}
});
fontImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
activity_image.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mobilesolution.imgedit.ImageActivity">
<ImageView
android:id="@+id/imageSetMain"
android:layout_width="336dp"
android:layout_height="478dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageBrush"
android:layout_width="67dp"
android:layout_height="38dp"
app:srcCompat="@drawable/brush"
android:layout_marginLeft="62dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="62dp"
app:layout_constraintRight_toLeftOf="@+id/imageFont"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.08"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/imageSetMain"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="@+id/imageFont"
android:layout_width="65dp"
android:layout_height="37dp"
app:srcCompat="@drawable/font"
android:layout_marginStart="8dp"
android:layout_marginEnd="83dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginRight="75dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/imageSetMain"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
现在主要的罪魁祸首文件在这里 --
我正在 select 中 MainActivity.java 中的图像,通过使用意图,我将图像路径传递给下一个 activity,称为 ImageActivity.java。
在 ImageActivity.java 中,我使用了 imageView.setImageUri(uri),无法在设备上看到图像。毕加索也一样。
我在下面添加了对 picasso 的依赖
compile 'com.squareup.picasso:picasso:2.5.2'
我还添加了清单文件的以下权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
任何帮助都将不胜感激。
尝试从文件路径制作位图。然后将位图设置为您的 ImageView。
String ImagePath = getIntent().getStringExtra("selectedImg");
Bitmap map = BitmapFactory.decodeFile(ImagePath );
mainImg.setImageBitmap(map);
我认为你应该检查下面的答案:
ACTION_PICK image picker returns diffrent uri formats
可能不同 Android OS 版本将 return 不同的内容 uri
Intent data = getIntent();
Uri selectedImage = data.getData();
Bitmap imageBitmap = null;
if(selectedImage != null && selectedImage.getAuthority() != null) {
//
InputStream is = null;
try {
is = getContentResolver().openInputStream(selectedImage);
imageBitmap = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
else {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageBitmap = BitmapFactory.decodeFile(picturePath);
}
最后,如果imageBitmap
不为null,用Picasso加载位图到ImageView或者直接加载。
我正在尝试 select 图片库中的图像并将其发送给其他人 activity。它在 Kitkat 中运行良好,但在 android Nougat 中不起作用。我也尝试了普通的 setImageUri 和 Picasso。仍然无法在 Imageview 上渲染图像。请看下面的代码。
- MainActivity.java - 我有一个按钮,点击按钮我调用 intent 打开图库。
gallery_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/"); startActivityForResult(intent, SELECT_PHOTO); } });
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PHOTO) {
Uri selectedImageUri = data.getData();
Log.d("vishal path", selectedImageUri.toString());
selectedImagePath = getPath(selectedImageUri);
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtra("selectedImg", selectedImagePath);
startActivity(intent);
}
}
}
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
// 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);
}
return uri.getPath();
}
通过上述方法,我能够从图库中获取图像路径。我可以在日志监视器上看到。
ImageActivity
public class ImageActivity 扩展 AppCompatActivity {
ImageView mainImg, brushImg, fontImg; String imgPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); mainImg = (ImageView) findViewById(R.id.imageSetMain); brushImg = (ImageView) findViewById(R.id.imageBrush); fontImg = (ImageView) findViewById(R.id.imageFont); mainImg.postInvalidate();; Bundle extras = getIntent().getExtras(); imgPath = extras.getString("selectedImg"); Log.d("vishal received path", imgPath); // mainImg.setImageURI(null); // mainImg.setImageURI(Uri.parse(extras.getString("selectedImg"))); Picasso.with(ImageActivity.this).load(new File(imgPath)).into(mainImg); brushImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ImageActivity.this, DrawActivity.class); intent.putExtra("selectedImg", imgPath); startActivity(intent); } }); fontImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); }
}
activity_image.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mobilesolution.imgedit.ImageActivity"> <ImageView android:id="@+id/imageSetMain" android:layout_width="336dp" android:layout_height="478dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="16dp" app:layout_constraintHorizontal_bias="0.507" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageBrush" android:layout_width="67dp" android:layout_height="38dp" app:srcCompat="@drawable/brush" android:layout_marginLeft="62dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp" android:layout_marginStart="62dp" app:layout_constraintRight_toLeftOf="@+id/imageFont" android:layout_marginRight="8dp" app:layout_constraintHorizontal_bias="0.08" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/imageSetMain" app:layout_constraintVertical_bias="1.0" /> <ImageView android:id="@+id/imageFont" android:layout_width="65dp" android:layout_height="37dp" app:srcCompat="@drawable/font" android:layout_marginStart="8dp" android:layout_marginEnd="83dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp" android:layout_marginRight="75dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/imageSetMain" app:layout_constraintVertical_bias="1.0" /> </android.support.constraint.ConstraintLayout>
现在主要的罪魁祸首文件在这里 --
我正在 select 中 MainActivity.java 中的图像,通过使用意图,我将图像路径传递给下一个 activity,称为 ImageActivity.java。
在 ImageActivity.java 中,我使用了 imageView.setImageUri(uri),无法在设备上看到图像。毕加索也一样。
我在下面添加了对 picasso 的依赖
compile 'com.squareup.picasso:picasso:2.5.2'
我还添加了清单文件的以下权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
任何帮助都将不胜感激。
尝试从文件路径制作位图。然后将位图设置为您的 ImageView。
String ImagePath = getIntent().getStringExtra("selectedImg");
Bitmap map = BitmapFactory.decodeFile(ImagePath );
mainImg.setImageBitmap(map);
我认为你应该检查下面的答案:
ACTION_PICK image picker returns diffrent uri formats
可能不同 Android OS 版本将 return 不同的内容 uri
Intent data = getIntent();
Uri selectedImage = data.getData();
Bitmap imageBitmap = null;
if(selectedImage != null && selectedImage.getAuthority() != null) {
//
InputStream is = null;
try {
is = getContentResolver().openInputStream(selectedImage);
imageBitmap = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
else {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageBitmap = BitmapFactory.decodeFile(picturePath);
}
最后,如果imageBitmap
不为null,用Picasso加载位图到ImageView或者直接加载。