当我使用传感器更改屏幕方向时,ImageView 在 android 中变为空白?
When I change screen orientation using sensor ImageView becomes blank in android?
这里我使用下面的代码
这里的问题是使用以下代码我从图库中挑选图像并在 ImageView 中设置但是当我旋转屏幕时 ImageView 变为空白。所以给我解决方案如何保存状态和显示图像。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//.setAction("Action", null).show();
//Intent i = new Intent(MainActivity.this,firstActivity.class);
//startActivity(i);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
在清单 activity 标记中使用它。
android:screenOrientation="nosensor"
当你改变方向时,如果你不添加
android:configChanges="keyboardHidden|orientation|screenSize"
在清单中 -> activity 将 re-create。请re-check吧。
时间轮换重启activity。必须防止重新启动。
在需要的manifest文件中添加如下代码activity
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
这里我使用下面的代码
这里的问题是使用以下代码我从图库中挑选图像并在 ImageView 中设置但是当我旋转屏幕时 ImageView 变为空白。所以给我解决方案如何保存状态和显示图像。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//.setAction("Action", null).show();
//Intent i = new Intent(MainActivity.this,firstActivity.class);
//startActivity(i);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
在清单 activity 标记中使用它。 android:screenOrientation="nosensor"
当你改变方向时,如果你不添加
android:configChanges="keyboardHidden|orientation|screenSize"
在清单中 -> activity 将 re-create。请re-check吧。
时间轮换重启activity。必须防止重新启动。
在需要的manifest文件中添加如下代码activity
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"