我有一个使用意图拍照的应用程序。 这是我的主要活动。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image, return media file
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
这是我的onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Log.d("onActivityResult",data.getData().toString());
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Log.d("Image", photo.toString());
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(this, "Image capture cancelled", Toast.LENGTH_LONG).show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image capture failed", Toast.LENGTH_LONG).show();
}
}
}
在清单中我请求了
的许可应用程序运行,允许你拍照,但一旦你接受图片我得到一个强制关闭错误,我不知道为什么。我使用此链接作为指导和大部分代码我得到了以下link并且我浏览了stackoverflow以查找类似问题,此链接是我能找到的最接近的应用程序related
我仍然关闭力量(拍照后,它不会返回应用程序)。 任何帮助将不胜感激。
整个班级如下
public class MyActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
public static final int MEDIA_TYPE_IMAGE = 1;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "DecodeM");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(this, "Image capture cancelled", Toast.LENGTH_LONG).show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image capture failed", Toast.LENGTH_LONG).show();
}
}
}
}
logcat错误:
06-13 09:29:04.123: DEBUG/SntpClient(74): request time failed:java.net.SocketException: Address family not supported by protocol
06-13 09:34:04.170: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 09:39:04.224: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 09:44:04.275: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 09:49:04.282: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 09:54:04.297: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 09:59:04.336: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:04:04.380: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:09:04.382: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:14:04.413: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:19:04.467: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:22:04.880: DEBUG/dalvikvm(209): GC_CONCURRENT freed 496K, 52% free 2981K/6151K, external 1625K/2137K, paused 7ms+11ms
06-13 10:24:04.521: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:29:04.584: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:34:04.601: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:39:04.629: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:44:04.640: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:49:04.697: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:54:04.720: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 10:59:04.746: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:04:04.757: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:09:04.786: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:14:04.795: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:19:04.859: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:22:00.270: DEBUG/dalvikvm(74): GC_CONCURRENT freed 1188K, 59% free 4352K/10375K, external 3520K/3903K, paused 8ms+11ms
06-13 11:24:04.906: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:29:04.960: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:34:04.962: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:35:46.359: INFO/jdwp(204): Ignoring second debugger -- accepting and dropping
06-13 11:35:46.482: DEBUG/dalvikvm(269): Debugger has detached; object registry had 1 entries
06-13 11:35:46.490: DEBUG/dalvikvm(338): Debugger has detached; object registry had 1 entries
06-13 11:35:46.550: DEBUG/dalvikvm(74): Debugger has detached; object registry had 1 entries
06-13 11:35:46.550: DEBUG/dalvikvm(320): Debugger has detached; object registry had 1 entries
06-13 11:35:46.560: DEBUG/dalvikvm(265): Debugger has detached; object registry had 1 entries
06-13 11:35:46.560: DEBUG/dalvikvm(209): Debugger has detached; object registry had 1 entries
06-13 11:35:46.641: INFO/jdwp(307): Ignoring second debugger -- accepting and dropping
06-13 11:35:46.641: DEBUG/dalvikvm(307): Debugger has detached; object registry had 1 entries
06-13 11:35:46.831: DEBUG/dalvikvm(204): Debugger has detached; object registry had 1 entries
06-13 11:35:47.400: INFO/jdwp(405): Ignoring second debugger -- accepting and dropping
06-13 11:35:47.419: DEBUG/dalvikvm(439): Debugger has detached; object registry had 1 entries
06-13 11:35:47.459: DEBUG/dalvikvm(364): Debugger has detached; object registry had 1 entries
06-13 11:35:47.489: DEBUG/dalvikvm(389): Debugger has detached; object registry had 1 entries
06-13 11:35:47.489: DEBUG/dalvikvm(416): Debugger has detached; object registry had 1 entries
06-13 11:35:47.489: DEBUG/dalvikvm(405): Debugger has detached; object registry had 1 entries
06-13 11:35:47.489: INFO/jdwp(285): Ignoring second debugger -- accepting and dropping
06-13 11:35:47.512: DEBUG/dalvikvm(285): Debugger has detached; object registry had 1 entries
06-13 11:35:47.632: DEBUG/dalvikvm(350): Debugger has detached; object registry had 1 entries
06-13 11:35:51.260: DEBUG/AndroidRuntime(586): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-13 11:35:51.260: DEBUG/AndroidRuntime(586): CheckJNI is ON
06-13 11:35:54.200: DEBUG/AndroidRuntime(586): Calling main entry com.android.commands.pm.Pm
06-13 11:35:54.680: DEBUG/dalvikvm(307): GC_EXPLICIT freed 3K, 54% free 2538K/5511K, external 1625K/2137K, paused 175ms
06-13 11:35:54.700: WARN/ActivityManager(74): No content provider found for:
06-13 11:35:54.850: WARN/ActivityManager(74): No content provider found for:
06-13 11:35:54.930: DEBUG/PackageParser(74): Scanning package: /data/app/vmdl585229931.tmp
06-13 11:35:55.383: INFO/PackageManager(74): Removing non-system package:com.decode.app1
06-13 11:35:55.390: INFO/ActivityManager(74): Force stopping package com.decode.app1 uid=10035
06-13 11:35:56.261: DEBUG/PackageManager(74): Scanning package com.decode.app1
06-13 11:35:56.270: INFO/PackageManager(74): Package com.decode.app1 codePath changed from /data/app/com.decode.app1-1.apk to /data/app/com.decode.app1-2.apk; Retaining data and using new
06-13 11:35:56.280: INFO/PackageManager(74): Unpacking native libraries for /data/app/com.decode.app1-2.apk
06-13 11:35:56.372: DEBUG/installd(34): DexInv: --- BEGIN '/data/app/com.decode.app1-2.apk' ---
06-13 11:35:57.260: DEBUG/dalvikvm(595): DexOpt: load 135ms, verify+opt 296ms
06-13 11:35:57.313: DEBUG/installd(34): DexInv: --- END '/data/app/com.decode.app1-2.apk' (success) ---
06-13 11:35:57.350: WARN/PackageManager(74): Code path for pkg : com.decode.app1 changing from /data/app/com.decode.app1-1.apk to /data/app/com.decode.app1-2.apk
06-13 11:35:57.350: WARN/PackageManager(74): Resource path for pkg : com.decode.app1 changing from /data/app/com.decode.app1-1.apk to /data/app/com.decode.app1-2.apk
06-13 11:35:57.360: DEBUG/PackageManager(74): Activities: com.decode.app1.MyActivity
06-13 11:35:57.430: INFO/ActivityManager(74): Force stopping package com.decode.app1 uid=10035
06-13 11:35:58.330: INFO/installd(34): move /data/dalvik-cache/data@app@com.decode.app1-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.decode.app1-2.apk@classes.dex
06-13 11:35:58.363: DEBUG/PackageManager(74): New package installed in /data/app/com.decode.app1-2.apk
06-13 11:35:58.389: WARN/PackageManager(74): Unknown permission android.permission.READ_EXTERNAL_STORAGE in package com.decode.app1
06-13 11:35:59.420: INFO/ActivityManager(74): Force stopping package com.decode.app1 uid=10035
06-13 11:35:59.780: DEBUG/dalvikvm(74): GC_EXPLICIT freed 607K, 59% free 4273K/10375K, external 3511K/3903K, paused 331ms
06-13 11:36:00.150: DEBUG/dalvikvm(265): GC_EXPLICIT freed 13K, 50% free 2960K/5895K, external 5863K/6816K, paused 269ms
06-13 11:36:00.770: DEBUG/dalvikvm(269): GC_EXPLICIT freed 142K, 53% free 2757K/5767K, external 1625K/2137K, paused 418ms
06-13 11:36:01.433: WARN/RecognitionManagerService(74): no available voice recognition services found
06-13 11:36:02.724: DEBUG/dalvikvm(74): GC_EXPLICIT freed 191K, 59% free 4255K/10375K, external 3511K/3903K, paused 556ms
06-13 11:36:03.570: INFO/installd(34): unlink /data/dalvik-cache/data@app@com.decode.app1-1.apk@classes.dex
06-13 11:36:03.641: DEBUG/AndroidRuntime(586): Shutting down VM
06-13 11:36:03.710: INFO/AndroidRuntime(586): NOTE: attach of thread 'Binder Thread #3' failed
06-13 11:36:03.740: DEBUG/dalvikvm(586): GC_CONCURRENT freed 100K, 72% free 294K/1024K, external 0K/0K, paused 4ms+16ms
06-13 11:36:03.760: DEBUG/dalvikvm(586): Debugger has detached; object registry had 1 entries
06-13 11:36:06.312: DEBUG/AndroidRuntime(598): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-13 11:36:06.312: DEBUG/AndroidRuntime(598): CheckJNI is ON
06-13 11:36:10.020: DEBUG/AndroidRuntime(598): Calling main entry com.android.commands.am.Am
06-13 11:36:10.170: INFO/ActivityManager(74): Starting: Intent { flg=0x10000000 cmp=com.decode.app1/.MyActivity } from pid 598
06-13 11:36:10.490: DEBUG/AndroidRuntime(598): Shutting down VM
06-13 11:36:10.590: INFO/AndroidRuntime(598): NOTE: attach of thread 'Binder Thread #4' failed
06-13 11:36:10.810: DEBUG/dalvikvm(598): GC_CONCURRENT freed 100K, 69% free 318K/1024K, external 0K/0K, paused 4ms+36ms
06-13 11:36:10.810: DEBUG/jdwp(598): Got wake-up signal, bailing out of select
06-13 11:36:10.810: DEBUG/dalvikvm(598): Debugger has detached; object registry had 1 entries
06-13 11:36:11.545: INFO/ActivityManager(74): Start proc com.decode.app1 for activity com.decode.app1/.MyActivity: pid=609 uid=10035 gids={1015}
06-13 11:36:16.250: INFO/ActivityManager(74): Displayed com.decode.app1/.MyActivity: +4s961ms
06-13 11:36:17.993: DEBUG/dalvikvm(307): GC_EXPLICIT freed 6K, 54% free 2538K/5511K, external 1625K/2137K, paused 10600ms
06-13 11:36:21.610: DEBUG/dalvikvm(405): GC_EXPLICIT freed 7K, 55% free 2591K/5703K, external 1625K/2137K, paused 201ms
06-13 11:36:26.761: DEBUG/dalvikvm(265): GC_EXPLICIT freed 76K, 50% free 2955K/5895K, external 5892K/6816K, paused 277ms
06-13 11:36:31.760: DEBUG/dalvikvm(439): GC_EXPLICIT freed 3K, 55% free 2531K/5511K, external 1625K/2137K, paused 230ms
06-13 11:39:05.040: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:39:18.269: DEBUG/MyCameraApp(609): failed to create directory
06-13 11:39:18.289: DEBUG/AndroidRuntime(609): Shutting down VM
06-13 11:39:18.289: WARN/dalvikvm(609): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-13 11:39:18.339: ERROR/AndroidRuntime(609): FATAL EXCEPTION: main
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:397)
at com.decode.app1.MyActivity.getOutputMediaFileUri(MyActivity.java:55)
at com.decode.app1.MyActivity.access$100(MyActivity.java:22)
at com.decode.app1.MyActivity$1.onClick(MyActivity.java:42)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
06-13 11:39:18.389: WARN/ActivityManager(74): Force finishing activity com.decode.app1/.MyActivity
06-13 11:39:19.009: WARN/ActivityManager(74): Activity pause timeout for HistoryRecord{405316b0 com.decode.app1/.MyActivity}
06-13 11:39:24.770: INFO/Process(609): Sending signal. PID: 609 SIG: 9
06-13 11:39:24.940: ERROR/InputDispatcher(74): channel '4096a3e0 com.decode.app1/com.decode.app1.MyActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
06-13 11:39:24.940: ERROR/InputDispatcher(74): channel '4096a3e0 com.decode.app1/com.decode.app1.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-13 11:39:24.950: INFO/ActivityManager(74): Process com.decode.app1 (pid 609) has died.
06-13 11:39:25.175: INFO/WindowManager(74): WINDOW DIED Window{4096a3e0 com.decode.app1/com.decode.app1.MyActivity paused=false}
06-13 11:39:25.310: WARN/WindowManager(74): Failed looking up window
java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@406366a0 does not exist
at com.android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:8174)
at com.android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:8165)
at com.android.server.WindowManagerService$WindowState$DeathRecipient.binderDied(WindowManagerService.java:7024)
at android.os.BinderProxy.sendDeathNotice(Binder.java:381)
at dalvik.system.NativeStart.run(Native Method)
06-13 11:39:25.310: INFO/WindowManager(74): WIN DEATH: null
06-13 11:39:25.790: WARN/InputManagerService(74): Got RemoteException sending setActive(false) notification to pid 609 uid 10035
06-13 11:39:32.667: WARN/ActivityManager(74): Activity destroy timeout for HistoryRecord{405316b0 com.decode.app1/.MyActivity}
06-13 11:44:05.170: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:49:05.180: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:49:46.323: INFO/dalvikvm(320): Jit: resizing JitTable from 512 to 1024
06-13 11:54:05.250: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 11:59:05.271: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
06-13 12:04:05.340: DEBUG/SntpClient(74): request time failed: java.net.SocketException: Address family not supported by protocol
答案 0 :(得分:0)
您可以像这样调用活动结果并获取图像的路径
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
并以这种方式打电话
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
try {
if ( resultData != null) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
String imagePath = cursor.getString(column_index_data);
Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath );
imageView.setImageBitmap(bitmapImage );
}
} catch (Exception ex) {
}
答案 1 :(得分:0)
而不是:
Bitmap photo = (Bitmap) data.getExtras().get("data");
在OnActivityResult中使用fileUri
变量值。
答案 2 :(得分:0)
你的功能
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
假设您从getOutputMediaFile
获得非空值。事实并非如此:有几种方法可以从中获取空值。这导致异常(至少Uri.fromFile(File file) documentation这样说。
所以,您可以将其更改为
private static Uri getOutputMediaFileUri(int type){
File fileToReturn = getOutputMediaFile(type);
return fileToReturn!=null?Uri.fromFile(fileToReturn):
null;
}