Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

java - on clicking the picture it should get stored in the internal storage of my mobile

I am newbie in creating android app.The problem is i am only able to open my camera after clicking the camera button but when i click the picture it does not get stored in my mobile anywhere.So i want that when i open my camera through the camera button i should be able to click a picture and able to store it in my mobile somewhere in an directory in the internal sorage of my mobile such as ("pitchers directory under some other folder name as (camera demo folder)")

my java file is-

public class B1ITEM1L3 extends AppCompatActivity {

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

    JSONArray jsonArray=getJSonData( "B1ITEM1DATA.json" );

    ArrayList<JSONObject> listItems=getArrayListFromJSONArray(jsonArray);

    ListView listV= findViewById(R.id.list_viewL3ITEM1);

    ListAdapter adapter = new ListadapterB1ITEM1 (this,R.layout.list_layoutb1item1,R.id.textView,listItems);

    listV.setAdapter(adapter);

}
private JSONArray getJSonData(String fileName){

    JSONArray jsonArray=null;

    try {

        InputStream is = getResources().getAssets().open(fileName);

        int size = is.available();

        byte[] data = new byte[size];

        is.read(data);

        is.close();

        String json = new String(data, "UTF-8");

        jsonArray=new JSONArray(json);

    }catch (IOException e){

        e.printStackTrace();

    }catch (JSONException je){

        je.printStackTrace();

    }

    return jsonArray;

}

private ArrayList<JSONObject> getArrayListFromJSONArray(JSONArray jsonArray){

    ArrayList<JSONObject> aList= new ArrayList<>();

    try {

        if (jsonArray != null) {

            for (int i = 0; i < jsonArray.length(); i++) {

                aList.add(jsonArray.getJSONObject(i));

            }

        }

    }catch (JSONException je){je.printStackTrace();}

    return  aList;
}

public void opencamera(View view) {
    Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
    startActivity( intent );
} }

my listadapter file is-

class ListadapterB1ITEM1 extends ArrayAdapter<JSONObject> {

int vg;

ArrayList<JSONObject> list;

Context context;

public ListadapterB1ITEM1(Context context, int vg, int id, ArrayList<JSONObject> list){

    super(context,vg, id,list);

    this.context=context;

    this.vg=vg;

    this.list=list;

}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(vg, parent, false);

    TextView txtId= itemView.findViewById( R.id.textView);

//    TextView txtName= itemView.findViewById(R.id.textView2);

    //   TextView txtSex=(TextView)itemView.findViewById(R.id.txtsex);

    try {

        txtId.setText(list.get(position).getString("id"));

  //      txtName.setText(list.get(position).getString("name"));

        //     txtSex.setText(list.get(position).getString("sex"));



    } catch (JSONException e) {

        e.printStackTrace();

    }



    return itemView;

}

}

main xml file is-

 <ListView
    android:id="@+id/list_viewL3ITEM1"
    android:layout_width="match_parent"
    android:layout_height="453dp"
    android:descendantFocusability="beforeDescendants">
</ListView>

<Button
    android:id="@+id/button"
    android:layout_width="136dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="2dp"
    android:text="save as draft"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/list_viewL3ITEM1"
    android:layout_marginStart="16dp"
    tools:ignore="HardcodedText" />

<Button
    android:id="@+id/button2"
    android:layout_width="136dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="2dp"
    android:text="cancel"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/list_viewL3ITEM1"
    android:layout_marginEnd="16dp"
    tools:ignore="HardcodedText" />

list layout xml file is-

 <CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="29sp"
    android:layout_marginEnd="4sp"
    android:layout_marginLeft="16sp"
    android:layout_marginTop="16sp"
    app:layout_constraintEnd_toStartOf="@+id/textView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginRight="4sp"
    android:layout_marginStart="16sp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="228dp"
    android:layout_height="100dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toStartOf="@+id/imageButton"
    app:layout_constraintHorizontal_bias="0.978"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/imageButton"
    android:onClick="opencamera"
    android:layout_width="78dp"
    android:layout_height="60dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_menu_camera"
    android:layout_marginEnd="16dp"
    tools:ignore="ContentDescription" />

<EditText
    android:id="@+id/editText"
    android:layout_width="345dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="textShortMessage"
    android:hint="Leave a Comment"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    android:layout_marginStart="24dp"
    android:layout_marginRight="16dp"
    tools:ignore="HardcodedText" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First open the camera Intent on button click

   if (checkSelfPermission(Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
           requestPermissions(new String[]{Manifest.permission.CAMERA},
                        MY_CAMERA_PERMISSION_CODE);
            } else {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
            } 
        }

In this method you will get your capture bitmap

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        storeImage(photo);

    }  
} 

Mehtod for storing bitmap as png in your internal storage

 private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
    Log.d(TAG,
            "Error creating media file, check storage permissions: ");
    return;
} 
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
} catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
}  
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...