【问题标题】:on clicking the picture it should get stored in the internal storage of my mobile单击图片时,它应该存储在我手机的内部存储器中
【发布时间】:2018-05-08 04:56:11
【问题描述】:

我是创建 android 应用程序的新手。问题是我只能在单击相机按钮后打开我的相机,但是当我单击图片时,它不会存储在我的手机中的任何地方。所以我希望当我打开时我的相机通过相机按钮我应该能够单击图片并能够将其存储在我的手机中某个目录中的某个目录中,例如(“其他文件夹名称下的“投手目录”为(相机演示文件夹) ")

我的 java 文件是-

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 );
} }

我的列表适配器文件是-

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;

}

}

主xml文件是-

 <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" />

列表布局xml文件是-

 <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" />

【问题讨论】:

    标签: java android json


    【解决方案1】:

    首先在按钮点击时打开相机Intent

       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);
                } 
            }
    

    在这个方法中你会得到你的捕获位图

     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);
    
        }  
    } 
    

    在内部存储中将位图存储为 png 的方法

     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());
    }  
     }
    

    【讨论】:

    • Faiz,我知道这组代码,但它在我的 java 文件中不起作用
    • 但是在你的 Activity 中你没有任何 onActivityForResult 方法
    • ??其中一个和含义是什么
    • B1ITEM1L3 在这个类中你打开相机的意图..但是你不接受来自这个意图的数据
    • 那么我应该在我目前的 java 文件中实现什么代码来实现上述存储图片
    猜你喜欢
    • 2012-02-07
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2012-06-27
    • 2012-07-18
    相关资源
    最近更新 更多