使用Environment.getExternalStoragePublicDirectory功能在android中通过sdcard发送多个图像

时间:2016-06-27 12:27:58

标签: android

下面找到java文件我不知道哪里出错但我的应用程序不幸停止了一些人可以帮助我在哪里以及哪一行我出错了..... 下面是我的logcat ...详情

  

致命的例外:主要                                                                                      处理:com.example.ankita.implicitintent,PID:16822                                                                                      显示java.lang.NullPointerException                                                                                          在com.example.ankita.implicitintent.MainActivity.onClick(MainActivity.java:82)                                                                                          在android.view.View.performClick(View.java:4651)                                                                                          在android.view.View $ PerformClick.run(View.java:19310)                                                                                          在android.os.Handler.handleCallback(Handler.java:733)                                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                          在android.os.Looper.loop(Looper.java:146)                                                                                          在android.app.ActivityThread.main(ActivityThread.java:5653)                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                                                          在java.lang.reflect.Method.invoke(Method.java:515)                                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291)                                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)                                                                                          at dalvik.system.NativeStart.main(Native Method)   06-27 17:17:40.025 16822-16822 / com.example.ankita.implicitintent I / Process:发送信号。 PID:16822 SIG:9

package com.example.ankita.implicitintent;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

    Button imgssend;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgssend=(Button)findViewById(R.id.images_id);

        imgssend.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        Intent i=null,chooser=null;
        switch (view.getId())
        {
            case R.id.images_id:
                File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String[] listofPictures=Pictures.list();
               Uri uri=null;
                ArrayList<Uri> arraylist=new ArrayList<Uri>();
                for(String picture:listofPictures)
                {
                    uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
                    arraylist.add(uri);
                }
                i=new Intent(Intent.ACTION_SEND_MULTIPLE);
                i.setType("image/*");
                i.putExtra(Intent.EXTRA_STREAM,arraylist);
                chooser=Intent.createChooser(i,"Send multiple");
                startActivity(chooser);
                break;
        }
    }
}


    here is xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.ankita.implicitintent.MainActivity">
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="@string/send_images"
        android:id="@+id/images_id"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

看起来像OnClick覆盖有问题。为什么不使用Button的onClick Listener? 像这样:

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button imgssend=(Button)findViewById(R.id.images_id);

    imgssend.setOnClickListener(new OnClickListener{
        @Override
        public void onClick(View v) {
            Intent i=null,chooser=null;
            File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String[] listofPictures=Pictures.list();
            Uri uri=null;
            ArrayList<Uri> arraylist=new ArrayList<Uri>();
            for(String picture:listofPictures)
            {
                uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
                arraylist.add(uri);
            }
            i=new Intent(Intent.ACTION_SEND_MULTIPLE);
            i.setType("image/*");
            i.putExtra(Intent.EXTRA_STREAM,arraylist);
            chooser=Intent.createChooser(i,"Send multiple");
            startActivity(chooser);
        }
    });
}
}