मुझे यह काम करने में थोड़ी देर लग गई और मैंने कई चीजें बनाई हैं और आखिरकार यह काम करती है। मैं निश्चित रूप से यह नहीं बता सकता कि मैंने जो कुछ किया है, वह समस्या का समाधान है, लेकिन वे सभी एक साथ कामकाजी समाधान बनाते हैं।
कैमरे की गतिविधि वापस नहीं लौटने के कई कारण हैं। दो प्रमुख इस प्रकार हैं: नई तस्वीर के लिए
पथ अमान्य, या गैर-मौजूद है, या यह
आवेदन को निलंबित कर दिया और बच गया पथ खो जाना नहीं बनाया जा सकता।
तो यहां मेरा कोड इन सभी समस्याओं को हल करने के लिए है, सभी एक साथ काम कर रहे हैं।
class ImageServices {
private static String getTempDirectoryPath(Context ctx) {
File cache;
// SD Card Mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/");
}
// Use internal storage
else {
cache = ctx.getCacheDir();
}
// Create the cache directory if it doesn't exist
if (!cache.exists()) {
cache.mkdirs();
}
return cache.getAbsolutePath();
}
public static Uri getOutputImageFileUri(Context ctx) {
// TODO: check the presence of SDCard
String tstamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(getTempDirectoryPath(ctx), "IMG_" + tstamp + ".jpg");
return Uri.fromFile(file);
}
}
कोड आंशिक रूप से developer.android.com द्वारा और Apache Cordova परियोजना के CameraLauncher class द्वारा आंशिक रूप से प्रेरित है:
सबसे पहले मैं सहायक वर्ग ImageServices
बनाया।
मेरी गतिविधि में बटन एक तस्वीर लेने के लिए ईवेंट हैंडलर इस तरह दिखता है: के रूप में imageFileUri पहले से ही विद्यमान फ़ाइल को इंगित करता है और आवश्यक प्रतिपादन onResume
में किया जाता है
private Uri imageFileUri;
private static final int MAKE_PHOTO_RESULT_CODE = 100;
private static final int PICK_PHOTO_RESULT_CODE = 101;
public void onMakePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFileUri = ImageServices.getOutputImageFileUri(this);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
Log.i("babies", "Taking picture: requested " + imageFileUri);
startActivityForResult(intent, MAKE_PHOTO_RESULT_CODE);
}
विधि onActivityResult
वास्तव में नहीं है, ज्यादा होते हैं विधि है, जो जब गतिविधि अग्रभूमि में वापस हो जाता है कहा जाता है:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode) {
case MAKE_PHOTO_RESULT_CODE:
assert imageFileUri != null;
break;
case ...
...other cases...
break;
}
}
}
लेकिन यह अभी भी पर्याप्त नहीं के रूप में imageFileUri
अपने ऐप को निलंबित होने पर खो जाए है। और नियमित डिवाइस पर संभावनाएं 100% के करीब होती हैं।तो अगली आप उदाहरण के राज्य के लिए imageFileUri
का मान संग्रहीत करना:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (imageFileUri == null) {
outState.putString("file-uri", "");
}
else {
outState.putString("file-uri", imageFileUri.toString());
}
};
और में फिर से लोड - सबसे आसान सीधे onCreate
में है: तो, फिर से,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null) {
String fileUri = savedInstanceState.getString("file-uri");
if (!fileUri.equals("")) imageFileUri = Uri.parse(fileUri);
}
}
कई अन्य समाधान के शीर्ष पर
- होशियार
getTempDirectoryPath
Apache Cordova से प्रेरित: इस साइट पर और साथ ही कहीं और प्रस्तुत किया, इसकी दो प्रमुख मतभेद हैं
और अब निलंबित आवेदन जीवित रहने के लिए अनुमति देता है imageFileUri
- कम से कम मेरे लिए - सब कुछ ठीक काम करता है।
आप 'toast.show(); 'at' atctivityResult()' को कॉल करना भूल गए। यह 'टोस्ट.मेकटेक्स्ट होना चाहिए (यह," सफलतापूर्वक पंजीकृत! ", टोस्ट.LENGTH_LONG) .show();' –