来自图库的图像未粘贴到 Fragment 中的 ImageView
Image from gallery is not pasted into ImageView in Fragment
我知道这里有很多类似的问题,但我的情况很愚蠢。或者我是。
所以,我有以下简单的布局:
XML结构:
<RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/image1"
android:contentDescription="Description" />
<TextView/>
<android.support.design.widget.TabLayout />
<android.support.v4.view.ViewPager />
<Button />
</RelativeLayout>
在按钮上单击图库打开,用户选择一些图像并将其放入 ImageView
我用下面的代码创建了测试 activity,它运行完美:
public class TestActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
imageView = (ImageView) findViewById(R.id.image1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == RESULT_OK) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
}
标记:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/image1"
android:layout_above="@+id/button1"
android:contentDescription="image" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Image"
android:id="@+id/button1"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
在原始项目中,此代码不起作用。 Gallery 被迅速调用,所有方法也被调用,但图像根本没有粘贴到 ImageView:
public class MainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chooserIntent = new Intent(Intent.ACTION_PICK);
chooserIntent.setType("image/*");
startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
imageView.setImageURI(uri);
Toast.makeText(getContext(), "Image is picked successfully", Toast.LENGTH_LONG).show();
}
}
到目前为止,我已经尝试在 READ_EXTERNAL_STORAGE
中使用以下代码片段(以防万一):
片段 1
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
片段 2
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
片段 3
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
} catch (IOException e) {
e.printStackTrace();
}
}
}
片段 4
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
暂无结果。我不知道可能是什么问题。如果有任何帮助,我将不胜感激。
谢谢
在发布问题 30 分钟后,我突然找到了答案
由于我的 ImageView 处于片段状态,因此当画廊启动并在图像拾取后立即重新创建时,它会被销毁。在重新创建 ImageView 时使用初始值绘制并且所拾取图像的 Uri 丢失
解决方案是将Uri 保存在某处,并通过条件语句将其应用于片段创建。或者以某种方式防止片段重新创建
希望对大家有所帮助
我知道这里有很多类似的问题,但我的情况很愚蠢。或者我是。
所以,我有以下简单的布局:
XML结构:
<RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/image1"
android:contentDescription="Description" />
<TextView/>
<android.support.design.widget.TabLayout />
<android.support.v4.view.ViewPager />
<Button />
</RelativeLayout>
在按钮上单击图库打开,用户选择一些图像并将其放入 ImageView
我用下面的代码创建了测试 activity,它运行完美:
public class TestActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
imageView = (ImageView) findViewById(R.id.image1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == RESULT_OK) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
}
标记:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/image1"
android:layout_above="@+id/button1"
android:contentDescription="image" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Image"
android:id="@+id/button1"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
在原始项目中,此代码不起作用。 Gallery 被迅速调用,所有方法也被调用,但图像根本没有粘贴到 ImageView:
public class MainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chooserIntent = new Intent(Intent.ACTION_PICK);
chooserIntent.setType("image/*");
startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
imageView.setImageURI(uri);
Toast.makeText(getContext(), "Image is picked successfully", Toast.LENGTH_LONG).show();
}
}
到目前为止,我已经尝试在 READ_EXTERNAL_STORAGE
中使用以下代码片段(以防万一):
片段 1
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
片段 2
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
片段 3
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
} catch (IOException e) {
e.printStackTrace();
}
}
}
片段 4
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
暂无结果。我不知道可能是什么问题。如果有任何帮助,我将不胜感激。
谢谢
在发布问题 30 分钟后,我突然找到了答案
由于我的 ImageView 处于片段状态,因此当画廊启动并在图像拾取后立即重新创建时,它会被销毁。在重新创建 ImageView 时使用初始值绘制并且所拾取图像的 Uri 丢失
解决方案是将Uri 保存在某处,并通过条件语句将其应用于片段创建。或者以某种方式防止片段重新创建
希望对大家有所帮助