startActivityForResult立即关闭

时间:2018-07-23 07:36:34

标签: java android firebase appcompatactivity startactivityforresult

我正在开发一个聊天应用程序,可以选择发送图片和文件。 问题是,当我单击按钮以启动startActivityForResult方法时,屏幕变白了半秒钟,然后返回到我所在的位置。 我在android模拟器上尝试了此程序,并且工作正常,但是如果在我的小米手机上尝试了,则无法正常工作(尽管之前已经工作了几周)。

为此,我使用以下代码:

public void onPickPhoto(View view) {
    if (checkPermissionREAD_EXTERNAL_STORAGE(context)) {
        Intent intent = new Intent(context, Chat_PrevisualizarArchivo.class);
        startActivity(intent);
    }
}
public boolean checkPermissionREAD_EXTERNAL_STORAGE(final Context context) {
    int currentAPIVersion = Build.VERSION.SDK_INT;
    if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                showDialog("External storage", context, Manifest.permission.READ_EXTERNAL_STORAGE);
            } else {
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

public void showDialog(final String msg, final Context context, final String permission) {
    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
    alertBuilder.setCancelable(true);
    alertBuilder.setTitle("Permission necessary");
    alertBuilder.setMessage(msg + " permission is necessary");
    alertBuilder.setPositiveButton(android.R.string.yes,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions((Activity) context,
                            new String[] { permission },
                            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                }
            });
    AlertDialog alert = alertBuilder.create();
    alert.show();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
            if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                Toast.makeText(Chat.this, R.string.permiso_lectura_denegado, Toast.LENGTH_SHORT).show();
            }
            break;
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
            if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                Toast.makeText(Chat.this, R.string.permiso_escritura_denegado, Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions,
                    grantResults);
    }
}

这将启动一个新活动。在该活动的onCreate方法中,我具有startActivityForResult。

public class Chat_PrevisualizarArchivo extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lay_preview_file);

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent,1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data != null) {
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedFile = data.getData();
                if (selectedFile != null) {
                    filePath = getPath(Chat_PrevisualizarArchivo.this, selectedFile);
                }
                String ext = filePath.substring(filePath.lastIndexOf("."), filePath.length());
            }
        }
    }
}

我已经读到这可能是由于活动启动模式所致,这是我的清单:

    <activity
        android:name=".Chat"
        android:launchMode="singleTop"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
    <activity
        android:name=".Chat_PrevisualizarArchivo"
        android:launchMode="singleTop"
        android:parentActivityName=".Chat"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Chat" />
    </activity>

按下按钮时的日志:

V/FA: Inactivity, disconnecting from the service
I/Timeline: Timeline: Activity_launch_request time:535826
V/FA: Recording user engagement, ms: 5276
      Connecting to remote service
V/FA: Activity paused, time: 798871
D/FA: Logging event (FE): user_engagement(_e), 
Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=5276, 
firebase_screen_class(_sc)=Chat, 
firebase_screen_id(_si)=4620370960915656203}]
V/FA: onActivityCreated
V/FA: Connection attempt already in progress
I/Timeline: Timeline: Activity_launch_request time:535898
D/FA: Logging event (FE): screen_view(_vs), 
Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Chat, 
firebase_previous_id(_pi)=4620370960915656203, 
firebase_screen_class(_sc)=Chat_PrevisualizarArchivo, 
firebase_screen_id(_si)=4620370960915656205}]
V/FA: Connection attempt already in progress
      Connection attempt already in progress
      Activity resumed, time: 798938
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@22de4a86 
time:535977
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 88
      Activity paused, time: 798959
D/FA: Logging event (FE): screen_view(_vs), 
Bundle[{firebase_event_origin(_o)=auto, 
firebase_previous_class(_pc)=Chat_PrevisualizarArchivo, 
firebase_previous_id(_pi)=4620370960915656205, 
firebase_screen_class(_sc)=Chat, 
firebase_screen_id(_si)=4620370960915656203}]
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3b9d55a7 
time:536254
V/FA: Activity resumed, time: 799233
V/FA: Inactivity, disconnecting from the service
V/FA: Recording user engagement, ms: 30379
V/FA: Connecting to remote service
V/FA: Activity paused, time: 829611
D/FA: Logging event (FE): user_engagement(_e), 
Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=30379, 
firebase_screen_class(_sc)=Chat, 
firebase_screen_id(_si)=4620370960915656203}]
V/FA: Connection attempt already in progress
D/FA: Connected to remote service

查看日志,我不知道它是否与Firebase有关,我用于应用程序的消息传递部分。

感谢任何帮助,谢谢!

0 个答案:

没有答案