แลกเปลี่ยนประสบการณ์และ ปัญหาการใช้งาน Mobile การพัฒนา App บน SmartMobile , tablet Android, iOS, Window Phone และ ฯลฯ
Moderator: mindphp, ผู้ดูแลกระดาน
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
จากโค้ด การใช้งานสามารถเลือกไฟล์ได้ แต่ไม่มีชื่อไฟล์ออกมาโชว์
โค้ดส่วนตัวแปร และการประกาศค่า
โค้ด: เลือกทั้งหมด
public class PDFtoPic extends AppCompatActivity {
EditText txtname;
TextView filename;
Button selecfile,PDFtoPic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfto_pic);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
txtname = findViewById(R.id.txtname);
filename = findViewById(R.id.filename);
selecfile = findViewById(R.id.btn1);
PDFtoPic = findViewById(R.id.btnwatertext);
โค้ดส่วน การเลือกไฟล์จากมือถือ และให้แสดงชื่อไฟล์นั้น
โค้ด: เลือกทั้งหมด
ActivityResultLauncher<Intent> sActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
Intent data = result.getData();
Uri uri = data.getData();
filename.setText(getfileName(uri,getApplicationContext()));
}
}
}
);
public void selectPDF(View view) {
Intent data = new Intent(Intent.ACTION_OPEN_DOCUMENT);
data.setType("*/*");
data = Intent.createChooser(data,"Choose file");
sActivityResultLauncher.launch(data);
}
String getfileName(Uri uri, Context cntxt){
String res = null;
if (uri.getScheme().equals("Con")){
Cursor cursor = cntxt.getContentResolver().query(uri,null,null,null,null);
try {
if (cursor != null && cursor.moveToFirst()){
res = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
if (res == null){
res = uri.getPath();
int cutt = res.lastIndexOf("/");
if (cutt != -1){
res = res.substring(cutt +1);
}
}
}
return res;
}
}
จะสามารถแก้ส่วนไหนให้สามารถโชว์ชื่อไฟล์ได้
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
ผลการรันไม่พบ Error
13:45 Build APK(s)
APK(s) generated successfully for 1 module:
Module 'PDF_Toolkits.app': locate or analyze the APK.
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
สอบถามการแสดงชื่อไฟล์ที่เลือก ออกมาโชว์ ใน JAVA Androind Studio
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
สอบถามการแสดงชื่อไฟล์ที่เลือก ออกมาโชว์ ใน JAVA Androind Studio
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
โค้ดหน้าฟอร์มค่ะ
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PDFtoPic">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="แปลงไฟล์ PDF เป็นภาพ"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView17"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center|right"
android:text="ชื่อผู้ใช้ : "
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:hint="กรุณาใส่ชื่อ"
android:inputType="textPersonName"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView6"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center|right"
android:text="เลือกไฟล์ PDF : "
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:hint="ชื่อไฟล์"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="selectPDF"
android:text="เลือกไฟล์ PDF" />
</LinearLayout>
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnwatertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="PDFtpic"
android:text="แปลงไฟล์ PDF เป็นภาพ" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
ฟอร์มที่ได้และการทำงานจะเป็นลักษณะนี้ค่ะ
- เมื่อรันหน้าสำหรับเลือกไฟล์ขึ้นมาจะมีลักษณะแบบนี้ค่ะ
- หน้าฟอร์ม.jpg (12.72 KiB) Viewed 2829 times
- เมื่อกดปุ่มเลือกไฟล์ PDF จะขึ้นให้เลือกไฟล์ PDF ที่มีอยู่ในเครื่องมือถือค่ะ
- เมื่อกดเลือกPDF.jpg (16.17 KiB) Viewed 2829 times
- แต่พอเลือกแล้ว ตัวแอปจะกลับมายังหน้าฟอร์มเดิม แต่ไม่มีการแสดงชื่อไฟล์ที่เลือกค่ะ
- เมื่อเลือกแล้วจะเด้งมาหน้าเดิม.jpg (12.72 KiB) Viewed 2829 times
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
ตอบเพิ่มเติม โค้ดส่วนที่ต้องการให้แสดงชื่อไฟล์ ออกมาโชว์ ใน JAVA Androind Studio
you're the only one treasure
-
fonfonn
- PHP VIP Members
- โพสต์: 4983
- ลงทะเบียนเมื่อ: 11/07/2022 9:28 am
โพสต์ที่ยังไม่ได้อ่าน
โดย fonfonn »
สามารถแก้ไขได้แล้วค่ะ โดย โค้ดในส่วน xml คือ
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PDFtoPic">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="แปลงไฟล์ PDF เป็นภาพ"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView17"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center|right"
android:text="ชื่อผู้ใช้ : "
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:hint="กรุณาใส่ชื่อ"
android:inputType="textPersonName"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView6"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center|right"
android:text="เลือกไฟล์ PDF : "
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:hint="ชื่อไฟล์"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="chosefile"
android:text="เลือกไฟล์ PDF" />
</LinearLayout>
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnwatertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="PDFtpic"
android:text="แปลงไฟล์ PDF เป็นภาพ" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
โค้ดในส่วน java คือ
โค้ด: เลือกทั้งหมด
public class PDFtoPic extends AppCompatActivity {
EditText txtname;
TextView filename;
Button selecfile,PDFtoPic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfto_pic);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
txtname = findViewById(R.id.txtname);
filename = findViewById(R.id.filename);
selecfile = findViewById(R.id.btn1);
PDFtoPic = findViewById(R.id.btnwatertext);
}
/// ปุ่มย้อนกลับ
@Override
public boolean onSupportNavigateUp() {
finish();
return super.onSupportNavigateUp();
}
ActivityResultLauncher<Intent> sActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
Intent data = result.getData();
Uri uri = data.getData();
filename.setText(getfilename(uri,getApplicationContext()));
}
}
}
);
public void chosefile (View view){
Intent data = new Intent(Intent.ACTION_OPEN_DOCUMENT);
data.setType("application/pdf");
data = Intent.createChooser(data,"Choose");
sActivityResultLauncher.launch(data);
}
String getfilename (Uri uri, Context context){
String res = null;
if (uri.getScheme().equals("content")){
Cursor cursor = context.getContentResolver().query(uri,null,null,null,null);
try {
if (cursor != null && cursor.moveToFirst()){
res = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
if (res == null){
res = uri.getPath();
int cut = res.lastIndexOf('/');
if (cut != -1)
{
res = res.substring(cut +1);
}
}
}
return res;
}
และทำการใส่โค้ดนี้ในหน้า AndrionManifest
โค้ด: เลือกทั้งหมด
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
สามารถเลือกไฟล์ และแสดงชื่อไฟล์ที่เลือกได้แล้วค่ะ
you're the only one treasure
-
-
- 0 ตอบกลับ
- 772 แสดง
-
โพสต์ล่าสุด โดย fonfonn
30/07/2022 3:23 pm
-
-
- 0 ตอบกลับ
- 1257 แสดง
-
โพสต์ล่าสุด โดย fonfonn
30/07/2022 4:15 pm
-
-
- 0 ตอบกลับ
- 5544 แสดง
-
โพสต์ล่าสุด โดย nuattawoot
06/06/2017 5:12 pm
-
-
- 0 ตอบกลับ
- 3240 แสดง
-
โพสต์ล่าสุด โดย Before Dong
03/10/2017 4:58 pm
-
-
- 0 ตอบกลับ
- 1977 แสดง
-
โพสต์ล่าสุด โดย hayatee
22/04/2021 1:58 pm
-
-
- 0 ตอบกลับ
- 683 แสดง
-
โพสต์ล่าสุด โดย วัชระ สินธุมาลา
02/11/2019 3:30 pm
-
-
- 0 ตอบกลับ
- 1388 แสดง
-
โพสต์ล่าสุด โดย nai_cyp
10/08/2020 7:10 pm
-
-
- 0 ตอบกลับ
- 4252 แสดง
-
โพสต์ล่าสุด โดย Grammanano
04/12/2019 1:48 pm
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 55