【发布时间】: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" />
【问题讨论】: