Android mDisplayList无效
我对。。。我真的不知道是什么导致了这个问题。。。 LogCat给了我这样的错误: 我能说什么…Android mDisplayList无效,android,fragment,Android,Fragment,我对。。。我真的不知道是什么导致了这个问题。。。 LogCat给了我这样的错误: 我能说什么… 应用程序中的操作是这样进行的: 我正在创建意图选择器,将packageManager.QueryInputActivities中的选项放入选择器。。。就这些 我正在从照相机或其他应用程序获取照片 我正在返回应用程序,此时我遇到了错误 我可以补充一点,拍一张照片是由活动中的两个片段中的一个诱导的 我能说的是,有时在我回到我的应用程序后,应用程序在加载时会出现阻塞 如果您需要更多信息,请告诉我。 任何关
应用程序中的操作是这样进行的:
任何关于搜索的提示都会非常有用(我在互联网上找不到这方面的信息) 编辑: 调用图像的片段:
public class AddAmbajFragment extends AbstractFragment implements View.OnClickListener {
Button addImageButton;
private Uri outputFileUri;
public AddAmbajFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_ambaj, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
assignViews();
setListeners();
}
public void assignViews(){
addImageButton = (Button) getView().findViewById(R.id.add_ambaj_camera_button);
}
public void setListeners(){
addImageButton.setOnClickListener(this);
}
@Override
protected void changeView(FragmentPlaceEnum fragmentPlaceEnum, FragmentEnum fragmentEnum){
if(changeFragmentEventListener!=null)
changeFragmentEventListener.onFragmenChange(fragmentPlaceEnum, fragmentEnum);
};
@Override
public void onClick(View view) {
openImageIntent();
}
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "AmbajePhotos" + File.separator);
root.mkdirs();
final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, getResources().getString(R.string.add_ambaj_select_source));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, 1);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
try {
if(resultCode == getActivity().RESULT_OK)
{
if(requestCode == 1)
{
final boolean isCamera;
if(data == null)
{
isCamera = true;
}
else
{
final String action = data.getAction();
if(action == null)
{
isCamera = false;
}
else
{
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
File imgFile = new File(selectedImageUri.getPath());
if (imgFile.exists()) {
Bitmap myBitmap = getScaledBitmap(imgFile.getAbsolutePath(), 800, 800);
ImageView myImage = (ImageView) getView().findViewById(R.id.add_ambaj_image_view);
myImage.setImageBitmap(myBitmap);
}
} else {
selectedImageUri = data == null ? null : data.getData();
File imgFile = new File(getRealPathFromURI(getActivity().getApplicationContext(), selectedImageUri));
if (imgFile.exists()) {
Bitmap myBitmap = getScaledBitmap(imgFile.getAbsolutePath(), 800, 800);
ImageView myImage = (ImageView) getView().findViewById(R.id.add_ambaj_image_view);
myImage.setImageBitmap(myBitmap);
}
}
}
}
}
catch(Exception e){
Log.w("KKK", "Error: "+e.toString());
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
if("content".equals(contentUri.getScheme())) {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else{
return contentUri.getPath();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private Bitmap getScaledBitmap(String picturePath, int width, int height) {
BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, sizeOptions);
int inSampleSize = calculateInSampleSize(sizeOptions, width, height);
sizeOptions.inJustDecodeBounds = false;
sizeOptions.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(picturePath, sizeOptions);
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
听上去,“有时它会崩溃”,我敢打赌你没有使用onSaveInstanceState()
和onRestoreInstanceState
这只是一种预感。如果你发布你的代码和日志,这将是很有帮助的;要检查这是否是问题的一部分,请在您的设备上,转到设置>开发人员选项>并选中“不保留活动”。如果您的设备未显示开发人员选项:
用于调整图像大小的异步任务示例
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.util.Log;
public class ImageResizerTask extends AsyncTask<Integer, Void, Bitmap> {
Bitmap mBitmap;
String filePath;
Context mContext;
YourActivity mCallBack;
//ProgressDialog pd;
public ImageResizerTask(Context context, String path,
YourActivity callBack) {
mContext = context;
filePath = path;
mBitmap = BitmapFactory.decodeFile(filePath);
mCallBack = callBack;
}
@Override
protected void onPreExecute() {
}
@Override
protected Bitmap doInBackground(Integer... params) {
// TODO Auto-generated method stub
resize(mBitmap);
return mBitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
bitmap.recycle();
mCallBack.onImageResized(filePath);
}
private void resize(Bitmap tmp) {
final Bitmap bitmap = Bitmap.createBitmap(500, 500,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
Log.v("TMPBMP",
"temp bmp width:" + tmp.getWidth() + " height:"
+ tmp.getHeight());
if (tmp.getWidth() < tmp.getHeight()) {
final int width = (int) (1f * tmp.getWidth() / tmp.getHeight() * 500);
final int height = 500;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = (bitmap.getWidth() - scaled.getWidth()) / 2;
final int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
} else {
final int width = 500;
final int height = (int) (1f * tmp.getHeight() / tmp.getWidth() * 500);
;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = 0;
final int topOffset = (bitmap.getHeight() - scaled.getHeight()) / 2;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
}
FileOutputStream outStream;
try {
outStream = new FileOutputStream(filePath);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.os.AsyncTask;
导入android.util.Log;
公共类ImageResizerTask扩展了AsyncTask{
位图mBitmap;
字符串文件路径;
语境;
你的活动McCallback;
//进展性帕金森病;
公共ImageResizerTask(上下文、字符串路径、,
您的活动(回调){
mContext=上下文;
filePath=path;
mBitmap=BitmapFactory.decodeFile(文件路径);
mCallBack=回调;
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护位图doInBackground(整数…参数){
//TODO自动生成的方法存根
调整大小(mBitmap);
返回mBitmap;
}
@凌驾
受保护的void onPostExecute(位图){
bitmap.recycle();
onImageResized(文件路径);
}
私有空间大小调整(位图tmp){
最终位图=位图.createBitmap(500500,
位图.Config.ARGB_8888);
最终画布=新画布(位图);
Log.v(“TMPBMP”,
“临时bmp宽度:”+tmp.getWidth()+“高度:”
+tmp.getHeight());
if(tmp.getWidth()
你正在打印那行吗?你的应用程序会崩溃吗?当我出错时不会。有时它会在返回应用程序后崩溃,但当时应用程序没有任何操作。没错-我没有保存状态,当我将活动更改为例如照相机时……我现在做了一个简短的测试,只有我能说的,在chan上应用程序之间的暂停事件始终为暂停/恢复(从不为恢复),当应用程序崩溃时,此处不会发生任何更改。我将尝试进行一些保存状态。我正在添加到我正在使用的主post image调用方法中。再次,请发布您的代码和日志。并不是说操作系统可以在内存不足时终止您的活动,然后当您从相机返回时,您将有空指针有日志(它们很大,所以我把它们放到另一台服务器上):按应用程序筛选:无筛选器:您需要哪部分代码?我想查看包含“mDisplayList”的活动代码。看起来确实是您的应用程序内存不足,就在“无效消息您将注意到您的日志”之前显示:GC_FOR_ALLOC freed 2K,4%free 18179K/18840K,暂停29ms,这意味着总共29ms您还有4%的内存,正在进行垃圾回收。我已经将其放入了第一个post.Fragment中,它正在调用映像,而活动包含Fragment。其中一个应该包含视图,这会导致错误。
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save things
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//restore things
}
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.util.Log;
public class ImageResizerTask extends AsyncTask<Integer, Void, Bitmap> {
Bitmap mBitmap;
String filePath;
Context mContext;
YourActivity mCallBack;
//ProgressDialog pd;
public ImageResizerTask(Context context, String path,
YourActivity callBack) {
mContext = context;
filePath = path;
mBitmap = BitmapFactory.decodeFile(filePath);
mCallBack = callBack;
}
@Override
protected void onPreExecute() {
}
@Override
protected Bitmap doInBackground(Integer... params) {
// TODO Auto-generated method stub
resize(mBitmap);
return mBitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
bitmap.recycle();
mCallBack.onImageResized(filePath);
}
private void resize(Bitmap tmp) {
final Bitmap bitmap = Bitmap.createBitmap(500, 500,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
Log.v("TMPBMP",
"temp bmp width:" + tmp.getWidth() + " height:"
+ tmp.getHeight());
if (tmp.getWidth() < tmp.getHeight()) {
final int width = (int) (1f * tmp.getWidth() / tmp.getHeight() * 500);
final int height = 500;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = (bitmap.getWidth() - scaled.getWidth()) / 2;
final int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
} else {
final int width = 500;
final int height = (int) (1f * tmp.getHeight() / tmp.getWidth() * 500);
;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = 0;
final int topOffset = (bitmap.getHeight() - scaled.getHeight()) / 2;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
}
FileOutputStream outStream;
try {
outStream = new FileOutputStream(filePath);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}