gpt4 book ai didi

Android:如何将图像加载到位图中

转载 作者:行者123 更新时间:2023-11-29 14:19:38 24 4
gpt4 key购买 nike

我可以从 drawable 文件夹加载资源图像到位图中,甚至可以很好地缩放,但是现在我需要从 SD 卡上存储的任何图像加载图像,我可以看到这将允许我查看和选择;

    startActivityForResult(new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);

但是到目前为止,我一直无法确切地找出返回的数据以及如何将其加载到位图中。

更新:

这就是我得到的;

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (resultCode == RESULT_OK) {

Uri contentURI = Uri.parse(intent.getDataString());

c = mSurfaceHolder.lockCanvas();

tempCanvas.setBitmap(mDrawing);

Bitmap tempBitmap = null;

ContentResolver cr = getContentResolver();

try {
InputStream in = cr.openInputStream(contentURI);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
tempBitmap = BitmapFactory.decodeStream(in,null,options);
}
catch (Exception ee) {
tempBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
}

tempCanvas.drawBitmap(tempBitmap, 0, 0, null);
c.drawBitmap(tempBitmap, 0, 0, null);

mSurfaceHolder.unlockCanvasAndPost(c);
}
}

它出错并显示“应用程序意外停止”并显示 logcat;

ERROR/AndroidRuntime(24768): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/185 }} to activity {com.example.colorbook/com.example.colorbook.Colorbook}: java.lang.NullPointerException


Complete logcat as it finished gallery view activity and tried to return intent;<br>
02-03 10:47:22.042: DEBUG/MainActivity(22984): [HTCAlbum][MainActivity][onPause]: Begin<br>02-03 10:47:22.042: DEBUG/MainActivity(22984): store mCurrentPosition_backup=0<br>02-03 10:47:22.042: DEBUG/DisplayManager(22984): store mCurrentPosition_backup=0<br>02-03 10:47:22.092: DEBUG/dalvikvm(22984): GC_EXPLICIT freed 1459 objects / 91880 bytes in 44ms<br>02-03 10:47:22.102: INFO/AlbumMapper(22984): Create thumbnail for: /mnt/sdcard/workspace/Colorbook/res/drawable/fileload.png<br>02-03 10:47:22.102: DEBUG/ThumbnailWorker(22984): [stopWorking] Stop working, now join #37, /mnt/sdcard/workspace/Colorbook/res/drawable/fileload.png<br>02-03 10:47:22.102: DEBUG/AlbumAdapter(22984): set thread priority to normal<br>02-03 10:47:22.102: DEBUG/AbstractViewGetter(22984): Raise getter prefetcher thread priority to normal.<br>02-03 10:47:22.102: DEBUG/AbstractViewGetter(22984): Join getter prefetch thread<br>02-03 10:47:22.102: DEBUG/AbstractViewGetter(22984): Getter prefetch join time: 2<br>02-03 10:47:22.102: DEBUG/AlbumMapper(22984): cancel current decode operation<br>02-03 10:47:22.182: DEBUG/AndroidRuntime(24768): Shutting down VM<br>02-03 10:47:22.182: WARN/dalvikvm(24768): threadid=1: thread exiting with uncaught exception (group=0x400259f8)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): FATAL EXCEPTION: main<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/185 }} to activity {com.example.colorbook/com.example.colorbook.Colorbook}: java.lang.NullPointerException<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread.deliverResults(ActivityThread.java:3734)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread.access$2800(ActivityThread.java:135)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.os.Handler.dispatchMessage(Handler.java:99)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.os.Looper.loop(Looper.java:144)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread.main(ActivityThread.java:4937)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at java.lang.reflect.Method.invokeNative(Native Method)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at java.lang.reflect.Method.invoke(Method.java:521)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at dalvik.system.NativeStart.main(Native Method)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): Caused by: java.lang.NullPointerException<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at com.example.colorbook.Colorbook.loadImageToCanvas(Colorbook.java:524)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at com.example.colorbook.Colorbook.onActivityResult(Colorbook.java:231)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.Activity.dispatchActivityResult(Activity.java:3931)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)<br>02-03 10:47:22.182: ERROR/AndroidRuntime(24768): ... 11 more<br>02-03 10:47:22.202: WARN/ActivityManager(97): Force finishing activity com.example.colorbook/.Colorbook<br>02-03 10:47:22.704: WARN/ActivityManager(97): Activity pause timeout for HistoryRecord{46207010 com.example.colorbook/.Colorbook}<br>02-03 10:47:22.742: DEBUG/Sensors(97): close_akm, fd=114<br>02-03 10:47:22.852: DEBUG/MainActivity(22984): [HTCAlbum][MainActivity][onStop]: Begin<br>02-03 10:47:22.902: DEBUG/dalvikvm(22984): GC_EXPLICIT freed 398 objects / 23264 bytes in 38ms<br>02-03 10:47:22.902: DEBUG/MainActivity(22984): [HTCAlbum][MainActivity][onDestroy]: Begin<br>02-03 10:47:22.912: DEBUG/OnlineDataCenter(22984): [HTCAlbum][OnlineDataCenter][unbindContext]: Begin<br>02-03 10:47:22.912: DEBUG/OnlineDataCenter(22984): [HTCAlbum][OnlineDataCenter][unbindContext]: com.htc.album.MainActivity@46075ec0context size: 0<br>02-03 10:47:22.912: DEBUG/OnlineDataCenter(22984): [HTCAlbum][OnlineDataCenter][unbindContext]: No more clients. release resources.<br>02-03 10:47:22.912: DEBUG/OnlineDataCenter(22984): [HTCAlbum][OnlineDataCenter][unbindContext]: End 0<br>02-03 10:47:22.912: DEBUG/ThumbnailWorker(22984): [stopWorking] Stop working, now join #-1, Decode Complete!!<br>02-03 10:47:22.912: DEBUG/AlbumAdapter(22984): set thread priority to normal<br>02-03 10:47:22.912: DEBUG/AlbumMapper(22984): cancel current decode operation<br>02-03 10:47:22.912: DEBUG/AlbumMapper(22984): cancel current decode operation<br>02-03 10:47:22.912: DEBUG/ThumbnailWorker(22984): [stopWorking] Stop working, now join #-1, Decode Complete!!<br>02-03 10:47:22.912: DEBUG/AlbumAdapter(22984): set thread priority to normal<br>02-03 10:47:22.912: DEBUG/AlbumAdapter(22984): Join worker thread in destroy<br>02-03 10:47:22.912: DEBUG/AlbumAdapter(22984): Join time: 0<br>02-03 10:47:22.922: INFO/MainActivity(22984): mWorker finishes jobs at onDestroy().<br>02-03 10:47:22.932: DEBUG/CollectionsActivity(22984): [HTCAlbum][CollectionsActivity][onDestroy]: Begin<br>02-03 10:47:22.932: DEBUG/AlbColAdap(22984): [HTCAlbum][AlbumCollectionsAdapter][onDestroy]: Begin<br>02-03 10:47:22.942: INFO/BatteryStatsImpl(97): notePhoneSignalStrengthLocked: 2->3<br>02-03 10:47:22.952: DEBUG/AlbColAdap(22984): [HTCAlbum][AlbumCollectionsAdapter][onDestroy]: End<br>

最佳答案

我使用这个 Intent :

myIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

然后用它来获取图像

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode){
case IMAGE_UPLOAD: //this is a constant, in your case I think it should be '1'
if(imageReturnedIntent != null){// e.g. "back" pressed"
Uri contentURI = Uri.parse(imageReturnedIntent.getDataString());
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(contentURI);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
Bitmap thumb = BitmapFactory.decodeStream(in,null,options);
}
break;

}

我稍微发送了那个字符串,但最后是这样的:

在本例中,您有图像的缩略图。如果你想要更大,使用不同的样本大小:)

关于Android:如何将图像加载到位图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4887297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com