我对将图像视图设置为相机拍摄的照片感兴趣。相机应用程序运行良好,并且将图像保存在sd卡中的适当位置,但是当我尝试加载图像并将其设置为图像视图时,它保持空白。除了使用最近拍摄的图像的路径之外,我还尝试对现有图像的路径进行硬编码,但是遇到了同样的问题。我检查了其他线程,但是看不到代码中的任何差异。
这是相机功能:
private void takePicture(){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI"+job_num);
image_name = username+"_"+date+".png";
File image_file = new File(imagesFolder, image_name);
while(image_file.exists()){
image_name = username+"-"+date+"("+ image_count+").png";
image_count+=1;
image_file = new File(imagesFolder,image_name);
}
image_path = imagesFolder+image_name;
Uri uriSavedImage = Uri.fromFile(image_file);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
int request_code = 100;
startActivityForResult(imageIntent, request_code);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
ImageView thumb = (ImageView) findViewById(R.id.thumbnail);
Bitmap bmp = BitmapFactory.decodeFile(image_path);
thumb.setImageBitmap(bmp);
Toast.makeText(this, "Image …Run Code Online (Sandbox Code Playgroud) 我有处理相机的片段.我的问题是它需要一张照片并将其显示在imageView上,就好了.
我第二次尝试拍摄一张OutOfMemory错误.任何提示如何在第一张照片后释放内存?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("tesstssaffsafdfsdfsd");
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getActivity().getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getActivity().getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
((ImageView)rootView.findViewById(R.id.snap)).setImageBitmap(mPhoto);
} catch (Exception e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
08-21 16:33:27.450: E/AndroidRuntime(1840): FATAL EXCEPTION: main
08-21 16:33:27.450: E/AndroidRuntime(1840): java.lang.OutOfMemoryError
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
08-21 16:33:27.450: …Run Code Online (Sandbox Code Playgroud) 我按照下面的说明裁剪图像.
http://coderzheaven.com/index.php/2011/03/crop-an-image-in-android/
最终裁剪的位图图像的高度和宽度都大于屏幕的高度和宽度.
在调整大小和裁剪过程中,抛出内存不足错误,并强制应用程序退出.
错误日志:
04-09 11:37:28.658: ERROR/dalvikvm-heap(4003): 435176-byte external allocation too large for this process.
04-09 11:37:28.658: ERROR/GraphicsJNI(4003): VM won't let us allocate 435176 bytes
04-09 11:37:28.668: WARN/dalvikvm(4003): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): FATAL EXCEPTION: main
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): at android.graphics.Bitmap.nativeCreate(Native Method)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): at com.test.Test.onCreate(Test.java:57)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 11:37:28.708: ERROR/AndroidRuntime(4003): …Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序,我使用Android相机拍照然后我查看照片,如果我喜欢它然后我上传到一个网站的图片.
将图片上传到网站我注意到有几个像素在手机上看不到!!!在网站上图片有一些额外的细节在手机屏幕上看不到!!!
电话屏幕上的图片设置在imageview中.
这是活动的布局:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:id="@+id/myPic"
/>
Run Code Online (Sandbox Code Playgroud)
<Button
android:text="Confirm Photo"
android:layout_toLeftOf="@id/back"
android:id="@+id/confirm"
android:layout_marginTop="530dip"
android:layout_height="wrap_content"
android:layout_width="165dip"
android:layout_alignParentRight="true"
>
</Button>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这就是我将图片设置为imageview:
Bundle extras = getIntent().getExtras();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize =2;
byte[] imageData = extras.getByteArray("imageData");
Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);
Matrix mat=new Matrix();
mat.postRotate(90);
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),myImage.getHeight(), mat, true);
Canvas c = new Canvas(bitmapResult);
drawTextImage(bitmapResult);
StoreByteImage(this, bitmapResult,100);
ImageView imageView = (ImageView) findViewById(R.id.myPic);
imageView.setImageBitmap(bitmapResult);
Run Code Online (Sandbox Code Playgroud)
对于额外的代码或其他细节,我在这里给你.谢谢
我正在尝试学习如何在网格视图中显示存储在Android手机上的所有图像(我的目标是让用户选择他们想要处理的图像).
我正在调整教程:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
这样做.
上面是我修改过的ImageAdapter类的链接(我认为唯一需要更改的内容).
在其中,我没有一个整数的资源ID数组,而是有一个媒体文件位置的字符串数组,并将它们变成一个位图,如下所示:
imageView.setImageBitmap(BitmapFactory.decodeFile(picture_paths[position]));
Run Code Online (Sandbox Code Playgroud)
我通过以下方式获取文件位置:
public void populateArray(){
Log.v("Jenny","Populating array");
picture_paths = new String[getCount()];
cursor.moveToFirst();
int count = 0;
while(!cursor.isLast() ){
count ++;
picture_paths[cursor.getPosition()] = cursor.getString(0);
cursor.moveToNext();
}
}
Run Code Online (Sandbox Code Playgroud)
它就像一个宝石,只要我有少于9张图片(三列三列).
除此之外,事情因"内存不足"错误而崩溃.如果我只是按照教程使用资源图像,我可以拥有尽可能多的东西.
那么:我是否采取了错误的方式?将它作为Bitmap加载到密集型?有什么替代品?
显示存储在手机上的图像似乎是一项非常简单的基本任务.做到这一点的"正确"方法是什么,无论如何我能用这种方式工作吗?
编辑:在我的第二部手机(Galaxy)上进行测试使得工作正常(可以显示任意数量的图像,尽管有时候光标检索的文件路径是'null').在我的原始测试手机(myTouch)上,无论我如何尝试显示图像(可绘制,位图等),我都会收到内存错误.为什么我会在两部手机上获得不同的行为?
编辑:嗯,有一件事可能会发生,我的myTouch上面有更多图片?我在Galaxy上拍了几张照片,现在也出现了内存错误......网格视图不是用于显示照片(如果没有,它有什么用处?)
仍然限制在myTouch上9,但在Galaxy上可以达到11
旋转时两者都会因内存不足而崩溃(即视图重绘).(虽然没有少量的照片(可能少于6张?)
在我的应用程序中,我正在显示来自厨房的图像和选择的图像我想将该图像上传到Web服务器.为了将图像上传到服务器我正在使用以下代码但是我在bitmapImage = BitmapFactory.decodeFile(路径)时收到错误 ,选择);
private void uploadImage(String selectedImagePath) {
String str = null;
byte[] data = null;
String Responce= null;
Bitmap bitmap2 = null;
try {
File file=new File(selectedImagePath);
//FileInputStream fileInputStream = new FileInputStream(new File(imagePath2) );
FileInputStream fileInputStream=new FileInputStream(selectedImagePath);
Log.i("Image path 2",""+selectedImagePath+"\n"+fileInputStream);
name=file.getName();
name=name.replace(".jpg","");
name=name.concat(sDate).concat(".jpg");
Log.e("debug",""+name);
//else
//{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
//bitmapImage = BitmapFactory.decodeFile(path,opt);
bitmap2=BitmapFactory.decodeFileDescriptor(fd, outPadding, opts)
Log.i("Bitmap",""+bitmap.toString());
BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
str=Base64.encodeBytes(data);
//} …Run Code Online (Sandbox Code Playgroud) 从sdcard或资源中选择大图像文件时避免内存不足错误.我该如何解决这个问题?
在我们的程序中使用大量图像,我们在eyery Activity和Fragment中取消绑定,如下所示
protected void unbindDrawables(View view) {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
imageView.setImageDrawable(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if (!(view instanceof AbsSpinner) && !(view instanceof AbsListView)) {
((ViewGroup) view).removeAllViews();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
像这样/sf/answers/474534721/说,但我使用mat发现该位图也使用了大量内存而不是发布.
Class Name | Objects | Shallow Heap | …Run Code Online (Sandbox Code Playgroud) 基本上我是为Android 4.4.2锁屏背景添加一个壁纸选择器,当图像设置后我关闭屏幕然后重新开启以查看锁屏我的屏幕变黑并且logcat给我一个内存不足分配错误.到目前为止,我已经尝试使用Bitmap decodeFile(String pathName),我也重新使用Bitmap decodeFile(String pathName,Options opts),但结果是每次都相同...
以下是用于设置图像的原始方法:
private static final String WALLPAPER_IMAGE_PATH =
"/data/data/com.android.settings/files/lockscreen_wallpaper.png";
private KeyguardUpdateMonitorCallback mBackgroundChanger = new KeyguardUpdateMonitorCallback() {
@Override
public void onSetBackground(Bitmap bmp) {
if (bmp != null) {
mKeyguardHost.setCustomBackground(
new BitmapDrawable(mContext.getResources(), bmp));
}
else {
File file = new File(WALLPAPER_IMAGE_PATH);
if (file.exists()) {
mKeyguardHost.setCustomBackground(
new BitmapDrawable(mContext.getResources(), WALLPAPER_IMAGE_PATH));
}
else {
mKeyguardHost.setCustomBackground(null);
}
}
updateShowWallpaper(bmp == null);
}
};
Run Code Online (Sandbox Code Playgroud)
这是从案例1中调用的:
public void setCustomBackground(Drawable d) {
if (!mAudioManager.isMusicActive()) {
int mBackgroundStyle = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.LOCKSCREEN_BACKGROUND_STYLE, 2);
int mBackgroundColor …Run Code Online (Sandbox Code Playgroud) 我正在尝试将大约80兆字节的文件从Android应用程序的assets文件夹复制到SD卡.
该文件是另一个apk.由于各种原因,我必须这样做,不能简单地链接到在线apk或把它放在Android市场上.
该应用程序适用于较小的apks,但对于这个大的,我得到一个内存不足的错误.
我不确定这是如何工作的,但我假设在这里我试图写入完整的80兆内存.
try {
int length = 0;
newFile.createNewFile();
InputStream inputStream = ctx.getAssets().open(
"myBigFile.apk");
FileOutputStream fOutputStream = new FileOutputStream(
newFile);
byte[] buffer = new byte[inputStream.available()];
while ((length = inputStream.read(buffer)) > 0) {
fOutputStream.write(buffer, 0, length);
}
fOutputStream.flush();
fOutputStream.close();
inputStream.close();
} catch (Exception ex) {
if (ODP_App.getInstance().isInDebugMode())
Log.e(TAG, ex.toString());
}
Run Code Online (Sandbox Code Playgroud)
我发现这很有趣 - 关于Bitmaps的内存不足问题
除非我误解了,在Bitmaps的情况下,似乎有一些方法可以使用BitmapFactory.Options拆分流以减少内存使用量.
这在我的方案中是否可行或是否有其他可能的解决方案?