Why is the app closing suddenly without showing an

2020-08-27 01:56发布

问题:

What my app does? The app is selecting a photo from photos Gallery.

What is my problem? Once I select the photo from gallery it closes up without error.

What I have done? I've increased the memory of the device and it didnt function. I took it out from proyect and the activity worked fine, it came back to the activity.

What is the main problem? It doesn't function on the proyect and I cant tell why.

Wold you like to see what is happening? Here is a video. First when it closes up, and second when I do it from another project and works fine.

http://www.youtube.com/watch?v=SntnyKiJQ1Q&feature=youtu.be


EDIT

My problem was that once I choose the picture from the gallery there was no activity on the stack.

Why?

The problem was on manifest's android:noHistory="true" tag, as there was no history the gallery couldn't find any activity and it closed the app.


The code:

The Intent I use to open the gallery and select a photo:

public void openGallery(int req_code){

   Intent intent = new Intent();
   intent.setType("image/*");
   intent.setAction(Intent.ACTION_GET_CONTENT);
   startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
}

The onActivityResult where I get the data which is never called because it closes up:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();
        if (requestCode == SELECT_FILE1)
        {
            selectedPath1 = getPath(selectedImageUri);
            System.out.println("selectedPath1 : " + selectedPath1);
        }
        if (requestCode == SELECT_FILE2)
        {
            selectedPath2 = getPath(selectedImageUri);
            System.out.println("selectedPath2 : " + selectedPath2);
        }
        tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
    }
}

The logcat:

01-05 15:18:14.569: I/ActivityManager(59): Starting activity: Intent {   act=android.intent.action.UPLOADIMAGEDEMO cmp=com.example/.UploadImageDemo }
01-05 15:18:15.779: D/dalvikvm(2496): GC_EXTERNAL_ALLOC freed 2697 objects / 189152 bytes in 92ms
01-05 15:18:16.599: I/ActivityManager(59): Displayed activity com.example/.UploadImageDemo: 1953  ms (total 1953 ms)
01-05 15:18:21.899: D/dalvikvm(279): GC_EXPLICIT freed 871 objects / 134112 bytes in 217ms
01-05 15:18:26.620: I/ActivityManager(59): Starting activity: Intent {  act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) }
01-05 15:18:28.250: I/ActivityManager(59): Starting activity: Intent {  act=android.intent.action.GET_CONTENT typ=image/* flg=0x3000000  cmp=com.android.gallery/com.android.camera.ImageGallery }
01-05 15:18:28.670: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 388 objects / 53352 bytes in 60ms
01-05 15:18:28.850: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 326 objects / 90664 bytes in 53ms
01-05 15:18:29.200: I/ActivityManager(59): Displayed activity    com.android.gallery/com.android.camera.ImageGallery: 873 ms (total 2345 ms)
01-05 15:18:29.420: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 725 objects / 106960 bytes in 140ms
01-05 15:18:30.430: W/InputManagerService(59): Starting input on non-focused client         com.android.internal.view.IInputMethodClient$Stub$Proxy@45020da8 (uid=10002 pid=279)
01-05 15:18:32.010: D/dalvikvm(112): GC_FOR_MALLOC freed 9994 objects / 476368 bytes in 149ms

I hope the community can help me!!

回答1:

Ok Guys, first of all thank you very much for your answers, they helped me to keep going today..

The problem was on manifest's android:noHistory="true" tag, as there was no history the gallery couldn't find any activity and it closed the app.

Again, thank you all.



回答2:

// Sorry, don't have enough reputation to comment accepted answer, but I guess my case will be helpful for others with the same issue

I had exactly the save issue in code written by another developer and struggled with it for a couple of days. But there was no android:noHistory attribute in manifest, but activity was launched with the flag FLAG_ACTIVITY_NO_HISTORY.

Like this:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

I googled for it for hours, reached this SO question twice, but found out an answer only when decided to search my whole project for word "history".



回答3:

Normally Android applications will get crashed if we are trying to load pictures as bitmap/drawable of huge size (depending upon heap memory allocated by os); for eg: 1mb sized images or more.

Try loading small sized images, and check whether the problem persist. Normally crash happens at code related to BitmapFactory.* .

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);


回答4:

It's hard to say what exactly is wrong cause trace does not contain any exception message. But I can share piece of code:

launch Intent

private final int GALLERY_REQUEST = 0;

private void runGallery() {
  Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(intent, GALLERY_REQUEST);
}

handle result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Uri selectedPictureUri = data.getData();
        loadGalleryAttachment(selectedPictureUri);
    }
}

private void loadGalleryAttachment(Uri uri){
    if (uri != null) {
        String path = getPath(uri);
        File f = new File(path);
        // do something with picture file   
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
    } 
}

Hope it will help you!



标签: android